Run the powershell file on windows 7. Powershell: how to work with the program, create, run and change scripts. Versions of Windows PowerShell

Golovna / Zahist

PowerShell has a number of wiping modes, which determine which type of code is allowed to witting. Everything is controlled by a registry key that lives in HKLM. Use 4 different modes of watching:

obmezheny(Restricted): The policy is by default, does not allow scripting and only allows interactive commands.

Must have signed(All Signed): Allows the robot to run all scripts. It's true, all scripts and configuration files must be signed by someone you trust; Danish mode you are punished for the risk of signing (ale shkіdlivih) scripts, after the confirmation of confirmation by the witnesses.

Vidal signatures(Remote Signed): Local scripts work without a signature. Must download scripts due to mother digital signature.

Non-furnishings(Unrestricted): Use scripts and configuration files, removed from communication programs, to Microsoft Outlook, Internet Explorer, Outlook Expressі Windows Messenger works after confirmation that you know that the file is coming from the Internet; none digital signatures do not need; This regime punishes you for the risk of robots and unsigned, shkidlivih scripts.

Behind the promotions for PowerShell is the "Flame" mode. In this mode, PowerShell works no more like an interactive shell. You do not allow scripts to work, and zavantazhu only configuration files, as if they were signed by a person who you trust.

If you take a drastic red pardon, then, as a rule, your appearance is due to itself, so you are trying to run an unsigned script. The most secure way to solve the problem is to change the policy to non-fenced, run the script, and then turn back to the fencing policy.

To change the policy to non-enclosed, run the lower-level command in administrative PowerShell:

Set-ExecutionPolicy Unrestricted

Please ask for confirmation. Press the Enter key.

Now you can run the quest script. However, you are facing a serious risk, so after finishing the robotic script, do not forget to turn the policy back to the outlier mode. You can do it for the help of the offensive team:

Set-ExecutionPolicy Restricted

I again ask for confirmation. Press the Enter key.

From i all! Good luck to you!

73095

І vichauchi menu Windows start 7 or cob screen Windows 8 1, koristuvachі-pochatkіvtsі often open programs for themselves, the recognition of which they did not understand. One such program is PowerShell itself. What is this PowerShell itself and what does it look like? Well, if you invented something and integrated it with Windows, it means that it's a good idea after all. Before speech, by PowerShell itself, what having come up with, є Microsoft.


Father, . This program is assigned, we are in front of the system administrators, and the axis is simple, it’s more budding, it’s not possible to get away from it for yourself. PowerShell is now a standardized shell command line, What will ensure the great flexibility of managing a computer on the basis of an operating room Windows systems. Essentially, PowerShell is the same command row, Ale, but richly functional.

What is the administrative tool? What a bagat. With the help of system administrators and informers, they can handle processes, services, file folders, accounts, and servers. Also, the shell allows you to run foreign commands practice with objects .NET, COMі XML, work different tricks, etc., create and win over your own scenarios. Before speech, for writing scripts in Windows, a special graphical console was transferred. ISE, which is a kind of addition to the main shell of PowerShell.

Yak and console cmd, the PowerShell shell can have its own set of commands, which are called cmdlets. The utility recognizes a lot of CMD commands, but in general, the syntax of the movie, the designation of cmdlets and their parameters are modified according to the fact that the shorthands sounded like a command string. In addition, know the commands dir , cd , copy and so on, as if miraculously understand PowerShell, in fact, pseudonyms. For example, the team Dir sho serve for revision file system respond to "native" cmdlet Get-ChildItem. Pseudonyms or as usual to say, alias, serve purely for the sake of clarity.

It will not be easy to learn the basics of a great command line, especially if you don't know the basics of a great command line. And posіbnikіv and dovіdnikіv, dosit zrozumіlih and simple for zavoєnnya, not so rich. Є indecent help, written by Frank Koch, we recommend that you familiarize yourself with it, so you still dare to proceed to the development of the PowerShell scripting movie. Well, it’s clear, the shell system is powerful.

Vaughn is torn and torn, with no specific butts. It's easy to finish koristuvatisya. Basic information about how the system can be displayed by the cmdlet Get Help. Cmdlet to display a list of available commands.

In this article, we can look at this technology in the Microsoft company like Windows PowerShell, we'll talk about what PowerShell is, what cmdlets and pipelines are, how to write scripts and modules, and also what other equally important and interesting features of Windows PowerShell are.

Before PowerShell appeared ( and base) Advanced tools for automation and administration of services: Windows command line and Windows Script Host. Ale and technology may have shortfalls.

At the command Windows rowsІсnuє and possibility to change the way administrative tasks and the possibility to write down the algorithm for diy, taking it from the script (bat-file), with which it is possible to change the programming elements, for example, to change the design cycles

Bigness software products you have a console interface, that's it. we can use the program, vicorist command row, while protecting the resources for the account of the daily allowance of the graphic interface for the robot. Microsoft for server operating system Windows Server navit vypuskaє editions without graphical interface ( Server Core, for Windows Server 2016 Nano Server), scarcely enough, shards of the ability of the command line of the fence, tobto. write as a folding logic for automating what is not possible, but if it is possible, then at the price we need an hour and knowledge.

Also, a small command line is those that for a full-scale service by the server mi vikoristovuemo different console utilities software products, and that's why it's really difficult to win a command line for administration, from the point of view of the fact that all utilities can use different syntax, parameters, methods of tricking and in order to win a typical task, tobto. schodenni, to be brought to mind all the same in memory, and vikonannya atypical tasks to be brought back to read the finalization of the documentation.

Windows Script Host technology allows you to hack all administrative tasks, including the command line, including automation by writing WSH scripts, but here we can also hack all the programming language ( VBScript and JScript), then. you can implement folding logic and algorithms. Before that, for the help of WSH, we need software products through an object interface, otherwise Windows words Script Host richly " cooler» lower command row. ale given technology also did not become an ideal tool for administration and automation of administrative administration for system administrators, because Windows Script Host needed to know the list of other programming languages, which for system administrators is a real application. Administrators need only a simple administrative tool with the ability to program like a code, but they don’t want to get lost in the object model of software products with VBScript and JScript programming.

As a result, Microsoft needed to develop such an administrative tool for system administrators, which satisfies all the needs of sysadmins for 100 thousand dollars, both in terms of administrative capabilities and automation, and in terms of efficiency and simplicity, PowerShell wins.

What is Windows PowerShell?

Windows PowerShell– this is the main script for the command shell of Windows, as it is expanded for administration and configuration of Windows operating systems. PowerShell is developed on the basis of the CRL and .NET Framework and on the command line view, which accepts and rotates text, Windows PowerShell works with objects. The PowerShell object can have the power to use methods that can be used to manipulate these objects.

Windows Microsoft PowerShell expanded the concept of cmdlets ( cmdlets), as a system of naming commands " Dієslovo-Іmennik". This system allows system administrators to more easily learn and work with PowerShell.

For help with Windows PowerShell, you can:

  • Retrieve access to the file system;
  • Cheruvati registry;
  • manage services;
  • Keruvati processes;
  • Change the operating system;
  • Install software security;
  • Install roles and server components;
  • Change administration and configuration of roles and server components;
  • Write and write scenarios for automating management and administration;
  • Vikonuvat іnshі heads of system administrators.

Windows PowerShell has a lot of frequently used utilities and commands that run from the command line, for example ipconfig, ping and others. It was broken in order to make it easier for system administrators to switch from the command line to PowerShell.

Also, for the sake of clarity, the commands and utilities of PowerShell can often be synonymous ( Alias), for example cls is a synonym for the Clear-Host cmdlet, dir is a synonym for Get-ChildItem ( complete list synonyms you can marvel at the launch of the Get-Alias ​​cmdlet).

To ask for a command in PowerShell, there is a special Get-Command cmdlet, for the help of which you can send a request in both a literal and a name. All Windows PowerShell commands are grouped in modules ( for example, Hyper-V, NetTCPIP), which also makes it easier to search for the required command.

Once the command is known, you can look at the instructions for working with the command, then. check, for which there is a special Get-Help cmdlet, for example, the following command will show the check on the Get-Command cmdlet:

Get-Help Get-Command

A summary in Windows PowerShell can be short, detailed ( parameter -Detailed), again ( -Full option), and you can also use it less ( parameter - Examples). The next command is to show more than just using the Get-Command cmdlet:

Get-Help Get-Command -Examples

PowerShell Advice on what to update. її It can be updated with the Update-Help command.

Versions of Windows PowerShell

The first version of PowerShell 1.0 was released on November 14, 2006 and was released in a seemingly okremenable distribution that can be installed on the next versions of Windows operating systems: Windows XP Service Pack 2, Windows Server 2003 Service Pack 1 and Windows Vista.

Windows Server 2008 PowerShell 1.0 was shipped as a component that needed to be installed as well.

Starting with Windows 7 and Windows Server 2008 R2, PowerShell is shipped as an invisible component of the system ( tobto. installations, installation yoga is not required). Below is a table showing the version of the PowerShell version and the version of the Windows operating system ( tobto. What is the promotional version of PowerShell installed in this version of Windows):

PowerShell version Windows versions
PowerShell 2.0 Windows 7, Windows Server 2008 R2
PowerShell 3.0 Windows 8, Windows Server 2012
PowerShell 4.0 Windows 8.1, Windows Server 2012 R2
PowerShell 5.0 Windows 10, Windows Server 2016

3 dermal new version PowerShell is becoming more and more of an intrusive administrative tool, there were close to 130 cmdlets for the first PowerShell, and PowerShell 5.0 has already more than 600!

Recognize current version PowerShell can help with the power of PSVersion in a modified $PSVersionTable, for example, enter the following command:

$PSVersionTable.PSVersion

Or run cmdlet

Get-Variable -Name PSVersionTable -ValueOnly

de value PSVersion and be the version of PowerShell.

Mova PowerShell

PowerShell– all object-oriented scripting language programming. Winning for writing commands for managing all components of the Windows operating system in the Windows PowerShell shell, as well as for writing scripts for automating administration tasks in the Windows PowerShell Integrated Scripting Environment (ISE). The language PowerShell was created for the administration manager, it is a complete scripted programming, shards are used for programming constructions, like in the skin language programming, such as: clean up, cycles, pardons, work with changes, upholstery.

The PowerShell language has a single syntax for writing commands and the structure of naming these commands for the principle " Dієslovo-Іmennik”, which is intuitively sensible both for programmers and system administrators.

Windows PowerShell shell

Windows PowerShell shell- This is the middle of the vykonannya commands and scripts on my PowerShell. Given the shell of maє tі zhlivosti, scho і the command row is like this: saving the history of the victorious commands, sane looking shells, completing the typing of commands using the Ctrl + C keys, as well as a lot of other possibilities, which are not available in the shell of the command row, for example, such a miraculous possibility like " Syntax matching» ( appeared in PowerShell 5.0).

You can start the PowerShell shell in a number of ways, for example:

  • From the command line, typing PowerShell;
  • Through the dialogue window Vikonati» ( Win+R keys), also by typing PowerShell;
  • For Windows 7 - Start->All programs ->Accessories ->Windows PowerShell -> Windows PowerShell;
  • For Windows 8.1 or Windows Server 2012 R2 - Start->All Programs->Services->Windows PowerShell;
  • For Windows 10 or Windows Server 2016 - Start->All Programs -> Windows PowerShell Directory ( in group W) -> Windows PowerShell.

An example of running PowerShell on Windows Server 2016

Windows Server 2016 PowerShell shell screenshot

PowerShell Cmdlets

Cmdlet (cmdlet) - this is a Windows PowerShell command, for the help of which you can interact with the objects of the operating system with the method of їhnoy keruvannya. Qi commands є partly move PowerShell. Cmdlets call for the principle " Dієslovo-Іmennik”, separated by a hyphen (-); In other words, we say at the beginning what to work, and through a hyphen over what. For example, the Get-Help cmdlet, de Get is a word, which means " Otrimati”, and Help is the name help» in PowerShell context « Show - Dovidka". PowerShell cmdlets rotate the results of objects that look like one of the main commands in the Windows command line, which commands rotate only the text on the screen.

Crim cmdlet otrimannya danih (Get), and so on types of cmdlets like:

  • Add - adding data;
  • Clear - clear;
  • Enable - turn on;
  • Disable - turn off;
  • New - create;
  • Remove - visualization;
  • Set - set;
  • Start - run;
  • Stop - Zupiniti;
  • Export - export;
  • Import - import;
  • And there are many others.

The full list of cmdlets in Windows PowerShell can be looked up for help with the special Get-Command cmdlet. For example, run yoga with the -CommandType cmdlet parameter, the result will show a list of cmdlets on the screen.

Get-Command -CommandType cmdlet

As you already understood, cmdlets can change parameters, for the help of which we can concretize each cmdlet. There are no language parameters and no language parameters, for example, the Get-Command cmdlet with no language parameters.

Below a small number of representations, the method of searching for a cmdlet using the word ( Verb parameter). At to this particular type we have a list of cmdlets that can be restarted.

Team

Get-Command -Verb Restart

To query a cmdlet by name, you need to select the Noun parameter. For example, we took a list of cmdlets that work with services.

Team

Get-Command -Noun Service

If you don't know the required cmdlet for the new name, you can change the mask in the *Text* format.

PowerShell Pipeline

One of the main features of Windows PowerShell is the ability to run a pipeline for running commands.

Conveyor– Do not pass the result of the robotic cmdlet through the vertical line (|) to another cmdlet. If so, as you remember, in PowerShell cmdlets work with objects and rotate objects, it seems that objects are also passed to the pipeline.

For the help of the conveyor, you can pick up folding orders, forgive me manually without the need to write folding algorithms and scenarios.

For example, let's find out the name of the largest file in the directory "C:\Windows\System32" ( simple conveyor example).

Team

Get-ChildItem -Path "C:\Windows\System32" -File | Sort-Object length -Descending | Select-Object -First 1

  • Get-ChildItem – cmdlet for removing objects from the specified catalog;
  • Sort-Object is a cmdlet for sorting objects; in our case, we sort by file size ( length -Descending);
  • Select-Object is a cmdlet for selecting the necessary properties of an object, in different cases, the standard fields and the first object, that is, are displayed. great file ( parameter -First 1).

Usі cmdlets vіdokremlenі one vіd one vertical boundary. The result of the skin cmdlet is passed to the input of another cmdlet, that is. We start by taking all the objects in the designated catalog, then sorting the result and choosing the first object for completion.

Fonone vikonannya zavdan

Windows PowerShell has the ability to run a background command, there is a mechanism, for the help of which you can run a command ( for example, for a long time) V background mode, then. After the start of the session, you turn to the current session and you can continue the work without checking the completion of the command. Given the opportunity for you to need it, if you ever need to run a command, the robot will take three hours to complete, and, as you know, for a long time, the session of the PowerShell shell will block until the robot command is completed, and you need to practice.

All tasks that are running in the background, you can check, for example, look over the list of tasks, zoom in on the robot of the task, see the task, and, obviously, look over the result of the work of the task.

Windows PowerShell for Background Jobs has the following cmdlets:

  • Start-Job - start a background job;
  • Stop-Job - Background Job Zupinity
  • Get-Job - look through the list of background jobs;
  • Receive-Job - look over the result of a background job typing;
  • Remove-Job - remove the background job;
  • Wait-Job - move the background task to the foreground, so that the check is completed.

To start in the background mode, you need to write the Start-Job command, and in curly arms () the command or a set of commands, as you need to write in the background mode.

For example, let's run a task like this ( show list of services) in the background mode, then look at the list of background tasks and display the result of our task on the screen ( tobto. service list).

Running a task in the background

Start-Job (Get-Service)

Looking at the list of tasks running in the background

Showing the result of Job1

Receive Job Job1

Yak bachimo, we have a task with a status. Completed”, then. she's already gone ( just Get-Service in good working order).

To marvel at the result of the work of the background task, tobto. of the Get-Service cmdlet, we hijacked the Receive-Job command and passed the name of the task ( possible i identifier value). As a result, we have seen a change of services.

Remote keruvannya on PowerShell

Windows PowerShell grants are not only limited to local selection, but also remote selection of commands. Tsya mozhlivist is necessary, so that you could zі svogo robochego mіstsya cheruvati vіddalennymi computers, tobto. concatenate PowerShell commands.

Іsnuє kіlka ways far away keruvannya:

  • For additional parameter -ComputerName (є for many teams). In other words, you pass the name of the computer to which you need to type a command, as a parameter. The way may not be enough, the shards are between the victors of one team;
  • For help session. Cmdlet Enter-PSSession (interactive session). In this way, you are connected to the remote computer and all the commands that you type in the PowerShell shell will be displayed on the remote computer just like that, you typed commands without interruption on the remote computer. The method is also small, since the session is surrounded by one computer;
  • Helping the cmdlet Invoke-Command. For help given method You can run commands either in scripts on one computer or on multiple computers.

For example, to connect to a remote computer ( at the application below ServerName) in an interactive session, type the following command:

Enter-PSSession ServerName

Scripts, Functions, and Modules in Windows PowerShell

For example, you have written some kind of algorithm, so for everything you want to save yoga, so that you can get better at it. Windows PowerShell has a scripting mechanism for which one.

The essence of this mechanism is coming, You write the commands you need and select it as a script - just a simple text file with extensions.PS1.

Let's see, to complete this script, you just run yoga in the PowerShell shell. If so, you need to specify the next path to the script file, or go to the directory with the script and click on the name.

Important!

Windows has been blocked for promotional scripts! To see the scripting policy, use the cmdlet Get-ExecutionPolicy. As a result, we will change the viable policy, for example:

  • Restricted - blocking any scenarios ( value for locking);
  • AllSigned - allowed to sign scripts, as a digital signature;
  • RemoteSigned – allowed to sign local scripts, all scripts involved are due to the digital signature;
  • Unrestricted - it is allowed to see any scenarios ( not recommended, shards are not safe!).

To revise the scenario, you need to revise the cmdlet Set-ExecutionPolicy with one of the repaid more parameters.

For example, in order to allow the local scripts to be broken, we will attack the command i wait for the changes made by pressing Y.

Set-ExecutionPolicy RemoteSigned

You can pass parameters to the script, make it work with language or set the value for the promotion.

In Windows PowerShell, the mechanism for creating powerful functions has been transferred, as well as in the case of cmdlets, you can hack in the PowerShell shell.

For whom it is necessary to indicate keyword Function and then at curly arches () write an algorithm to work with this function, tobto. set of commands ( for example, as a procedure that often happens: create a root account with sing rights, clean up sing directories, and so on). Then you need to save everything in the script, but also with extensions.psm1, the shards of this file will be a module.

Not only that, you need to place the entire file in the directory where PowerShell searches for modules. There are a few such catalogs ( special directory at koristuvach profile, directory where PowerShell is installed), you can marvel at the changes in PowerShell. For whom, let's attack the command

Get-ChildItem Env:\PSModulePath | Format-Table -AutoSize

Once you place the module you wrote in a special directory, you can navigate to your functions just like the original PowerShell commands.

Windows PowerShell Integrated Scripting Environment (ISE)

In order to manually write scripts, functions and modules, Microsoft has developed a special graphic program Integrated Scripting Environment(ISE) is an integrated scripting medium. Pratsyuvati in this program is already easy, before that there can be a hard functional. creation of anonymous tabs with scenarios, viewing area, opening the owner and more).

You can run її like this:

  • For Windows 7 - Start->All programs ->Accessories ->Windows PowerShell -> Windows PowerShell ISE;
  • For Windows 10 or Windows Server 2016 - Start->All Programs -> Windows PowerShell Directory ( in group W) -> Windows PowerShell ISE.

Note! ISE will not work on Windows Server systems installed on the Server Core variant.

Screenshot of Integrated PowerShell Scripting Environment (ISE) on Windows Server 2016

On whom I have everything, I support, I will correct the material for you! Success!

Updates to the Windows 10 operating system could be a bit about the PowerShell command line utility. Also, with it, you can know who can work with Windows 7 and 8.1. After the rich fate of Windows command line programs and files in the .bat format, it's time to learn more about a more functional tool.

PowerShell є tsіnim popupnennyam list Windows Tools and yogo scale can be zlyakati deyaky koristuvachіv. What is a scripted mov chi command shell? Don’t be turbulent: no matter how great the opportunity, master PowerShell under the power of the skin.

Krok 1: installed

For the cob, we need the PowerShell utility itself. If you are running Windows 10, you already have PowerShell 5 installed. Windows update 10 Anniversary Update victorious version 5.1, but the difference is not clear. Windows 8 and 8.1 win PowerShell 4, which is quite enough. Installing PowerShell on Windows 7 won't be a problem, but you'll still need to do a few tricks. Zocrema, you need to install the .Net Framework. You can install the Windows Management Framework, which includes PowerShell.

PowerShell has two interfaces. Additional information can be selected graphical interface, like the Integrated Scripting Environment (ISE) It's better to use the PowerShell console, a simple text interface in the Windows command line style or to type DOS 3.2.

To run PowerShell as administrator on Windows 10, press the Start button and scroll through the list of programs to Windows PowerShell. For Windows 8.1, look for Windows PowerShell under the System folder under Windows. In Windows 7, the shell is located in the Accessories folder. In the capacity of a great powershell, you can launch PowerShell just like that, using the left mouse button instead of the right one.

In any version of Windows, you can stop the search. For the convenience of a distant sorcerer, you can close PowerShell on the task panel.

Krok 2: old Windows commands

Syntax Windows commands PowerShell pratsyuє zvichny rank. For example, cd change folders, dir Shows a list of current files and folders in the middle of the stream folder.

If you use the method to launch the PowerShell console, you can c:\Windows\system32 or in c :\Users\ . At the butt on the screenshot, the team wins cd..(with a gap) to go up one line at a time, then run the command dir to display the list of files and folders in the directory C:\.

Krok 3: installing help files

Command nachebto cdі dir not active PowerShell commands. These are the names of aliases (aliases) - replace the right PowerShell commands. Pseudonyms are useful for those who can have a great opportunity to work in the command line. However, the stench does not stick around with the PowerShell glybin.

To get started with the power of PowerShell, type help I'll need a command for you. The screenshot shows the command .

Team help say what dirє PowerShell command alias Get-ChildItem. How to dial get-childitem V PS C:\>, indulge yourself, what and when vikoristanny commands dir.

As indicated at the bottom of the screenshot, the PowerShell help files are not automatically installed. To do this, run PowerShell as administrator and type update-help. Installed files to help borrow a small amount of fluff, a number of modules can be used daily - for example, Help for NetWNV and SecureBoot were not installed in this case. If everything is ready, the next system will help you to give you the necessary tips.

Now dial a command get-help and be it for you to command (“cmdlet” my PowerShell, Russian cmdlet), you will be shown її description. For example, get-help get-childitem see a list of options get-childitem. You can also enter different options. For example

get-help get-childitem -examples

see sim detailed examples get-childitem. Team

get-help get-childitem -detailed

include cі sim applications and reports clarification of the skin parameter in the cmdlet get-childitem.

Krok 4: otrimannya help for the parameters

On the screenshot you could remember two lists of pid Syntax For get-childitem. The presence of two different syntaxes means that two ways of running a cmdlet can be loaded. How to trim syntax okremo one type one and what do the parameters mean? The answer is simple, if you know the trick.

For otrimannya, detail the parameters of the cmdlet get-childitem otherwise, any other twist parameter -Full:

get-help get-childitem -full

Here you see a list of what you can do with the cmdlet and what will happen. See screenshot.

Reviewing the description of the parameters allows you to remember that get-childitem gives the ability to take the object child(Specifically name the subfolders or the file name) in the specified area, with a large number of sing symbols or without. For example:

get-childItem "*.txt" -recurse

rotate the list of *.txt files in the current folder and all subfolders (via the parameter -recurse). Todiyak

get-childitem “HKLM:\Software”

turn the list of used keys to the registry of the upper level HKEY_LOCAL_MACHINE\Software.

If you tried to go to the registry for the help of the Windows command line or the .bat file, you can evaluate the functionality of this access option.

Krok 5: names

Є the reason why the shown cmdlets dosі look like this: get-childitem, update-help, get-help vikoristovuyut a single scheme of the word-name. For the sake of zastosovuyut all PowerShell cmdlets, for which it is necessary to stand in front of a single name. It is worthy of a team who has suffered in his time due to the inconsistency of the names of the commands in the language VB and VBA.

Take a look at the most common cmdlets:

set location: install current working location on the first location

get-content: take in file

get-item: Remove files and folders

copy-item: copy an object from one location to another

remove-item: Delete files and folders

: Ignore processes running on local or remote computer.

get service: remove services, run on local or remote computer.

invoke-webrequest: take away from the web site on the Internet

To look over the robot of the singing cmdlet, sing get-help yak at the tip

get-help copy-item -full

Based on the description of the help, you can understand what cmdlets are needed. For example, if you want to copy all files and folders from Documents V c:\temp, vicorist

copy-item c:\users\ \documents\* c:\temp

Vvіvshi tsyu team, you chіlka kіlka tsіkavih possibilities otochennya PowerShell. For example, how to dial copy-i then press the Tab button, PowerShell Copy Item. If the cmdlet is typed incorrectly and PowerShell cannot recognize it, a new description of what was broken wrong is given.

Try this cmdlet:

invoke-webrequest askwoody.com

You will remove a short list of headings, images, messages and others on the web page. Give respect to get-help to the list invoke-webrequest, which "turns the collection of forms, send, images and other important HTML elements"- the very ones that can appear on the screen.

Deyakі cmdlet help keruvati themselves PowerShell:

get-command: list of available cmdlets

get-verb: list of currently available drives

clear-host: clear host screen

Different parameters allow you to change commands and ring brown options. For example, to review the list of all cmdlets that work with Windows services, enter

get-command *-service

All the words available with the name will be shown service. Axis x list:

Get Service

New Service

Restart Service

Resume Service

Set Service

start-service

Stop Service

Suspend Service

You can send more cmdlets with others.

Krok 6: vikoristanny pipes

What you know about the Windows command line or batch files, then you know about the redirection of that pipe. Redirect (symbol >) and pipe (symbol |) take the result of di and attach it in another place. For example, you can redirect the result of a command dir have a text file or pass the result of the command ping have a team find for filtering the cіkavih results, like

dir > temp.txt

ping askwoody.com | find "packets" > temp2.txt

Here at the other team find joking row packets taken from askwoody.com addresses by the team ping and combine all the rows that are running, I will name the file temp2.txt.

The first of these commands works miraculously with PowerShell. To run another command, you need to click on the

ping askwoody.com | select string packets | out-file temp2.txt

The redirection of pipes significantly expands the possibilities of the Windows command line: instead of scrolling down the screen at the request of the text line, you can filter the necessary Windows commands.

Powershell can help pipe, moreover, it is surrounded by text. PowerShell allows you to pass a whole object from one cmdlet to another, de ob'єkt є a combination of data (titles of authority) and dіy (methods), so that you can win over data.

The folding part is repaired under the hour of vibudovuvannya ob'ektiv. Objects that are supplied by one cmdlet are due to the type of objects that are accepted by the containing cmdlet. The text is already a simple type of objects, so if you work with the text, viewing objects is easy to do. Other objects are not so elementary.

How can you understand? Victory cmdlet get-member. If you want to know what type of object the cmdlet is processing, pass it through get-member. For example, as you can understand the processes running on the computer and sounding options before the cmdlet , axis how to recognize the result of the cmdlet:

get process | get-member

Run this cmdlet and see a long list of authorities and methods for , but on the cob of the list you can add the type of object that you create :

TypeName: System.Diagnostics.Process

Screenshot below also shows power under the name get-process Handles, Name, NPM, PM, SI, VMі WS.

How can you manipulate the result to work with a cmdlet (replacement of the old list of active processes on the monitor), you need to know another command, so that you can receive input data System.Diagnostics.Process. To find a useful cmdlet, revisit the possibilities of PowerShell:

get-command -Parametertype System.Diagnostics.Process

This cmdlet is a list of cmdlets that can be processed System.Diagnostics.Process.

Deyakі commandlet vіdomі tim, scho nabuvayut mayzhe be-what kind of data. Head middle є . This cmdlet passes through itself a skin object, which is sent through the pipe, one by one, and zastosovu to the new specified selection criteria. Use a special marker under the name $_ , which allows you to beat the leather object at the pipe, once.

Perhaps you want to take a list of all processes running on the computer under the name "svchost", so you want to establish power Name process svchost. Victory the command:

get process | where-object ($_.Name -eq “svchost”)

cmdlet marvel at the skin object System.Diagnostics.Process, match .Name th object from "svchost"; yakscho є zbіgi, the stench is seen monitor. See screenshot.

Lesson 7: Analysis of Core PowerShell Commands

Until now, you already know to finish the bagato, so that you can zoom in on the computer, so be careful. Look, for example, PowerShell commands, which are often requested.

These commands work only on Windows 10 and less when running PowerShell as administrator. The stinks are marked for reinstallation installed addenda Windows 10 and can become a good time for those who have uninstalled these programs, and then turned them off. The command looks like this:

Get-AppXPackage | Foreach (Add-AppxPackage -DisableDevelopmentMode -Register “$($_.InstallLocation)\AppXManifest.xml”)

If you run this command, ignore the warnings indicated by the red color ahead of time, and if the command is wicked, restart the computer; all installed programs Windows 10 will show up in its place.

The axis is how the team works. Get-AppXPackage reviewing all software packages in the profile of a koristuvach. Navіt yakscho vy deleted the program, it's out of the list of the profile of the koristuvach.

cmdlet Get-AppXPackage rotate object TypeName Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage, which includes outside the name of the program package and the scope of the XML manifest file. How to run the cmdlet get-appxpackage, you can see a long list of software packages. The screenshot shows the description of the Xbox.

cmdlet Foreach for an additional cycle to pass through the skin object AppXPackage, guiding the cmdlet Add-AppxPackage. Zgidno get-help For Add-AppxPackage, here there are two key switches:

  • peremikach -Register wink for registration of basic installations of software packages, you can set parameters Disable DevelopmentModeі Register
  • peremikach -DisableDevelopmentMode Every Windows needs to re-register the existing software package, which has been disabled, not registered, or disabled.

Row " $($_.InstallLocation)\AppXManifest.x ml" describes decompression file manifest.xml. How to look at files AppXManifest.xml, you can see a collapsible list of identifiers for add-ons, files that have been identified, and a large number of visual elements associated with add-ons.

After re-advancement, all added software packages are re-advanced and restored Windows Store store.

Launching a program from PowerShell

The task to launch from PowerShell such a file (a program or a command line utility) is often completed. PowerShell preach for which sprat different ways, as we can see in this article. Let's start with the simplest ...

direct launch

The simplest way to run the executable PowerShell file is to go to the directory with the file and start it directly. For example, let's take a simple program to run the air, and we can use the command:

Set-Location ″C:\Program Files″
.\Hello.exe

To return the respect, which is to be rebuilt in the necessary catalog, it is necessary to indicate the external path to the file, which is to be pointed out. Blame to install files from directories, refurbished at the changed sharpened (path). For example, various programs and utilities (notepad, calc, ping etc.), which are located in the Windows\System32 directory, can be run without opening the path.

& operator

If it is necessary to indicate the next path to the file to be checked, then you can speed it up with the operator & (Operator vikliku). Win allows vikonati a row of text, instructions at the paws like a single command. For example:

& 'C:\Program Files\Hello.exe'

If the & operator does not parse the command that is being sent, then vin cannot interpret the parameters. That additional parameters/arguments are also transmitted to the visual text, to the paws. For the butt, we take the program in front and change it a little bit, so we take the necessary text from the argument:

& 'C:\Program Files\Hello.exe' 'Hello, world'

For bajannya, you can specify a number of arguments through whom:

& 'C:\Program Files\Hello.exe' 'Hello,', 'world'

For clarity, the command and argument can be placed in the change:

$exe = 'C:\Program Files\Hello.exe'
$arg1 = 'Hello'
$arg2 = 'world'
& $exe $arg1 $arg2

Well, even though there are a lot of arguments, they can be combined by concentrating with such a construction:

$exe = 'C:\Program Files\Hello.exe'
$allargs = @('Hello,','world')
& $exe $allargs

Invoke-Expression

The Invoke-Expression cmdlet works in much the same way as the & operator takes a text string and takes it from the command it looks like. For example:

Invoke-Expression -Command 'C:\Windows\Hello.exe'

However, vin may have one great shortfall, and she herself will inevitably work with problems. For example, the team called for a pardon:

Invoke-Expression -Command 'C:\Program Files\Hello.exe'

What is special about squeezing the cmdlet to the edge of the handless. If you want such pardons, you can hide behind the help of appendage paws, for example:

Invoke-Expression -Command C:Program Files Hello.exe

start-process

The Start-Process cmdlet starts a file that looks like a process using a shortcut method Start.NET class process. For example:

Start-Process -FilePath 'C:\Program Files\Hello.exe'

For the locking process, the process wins over to the okrem, as it closes after the process is completed. You can change this behavior for additional parameters, so the command to start at the stream window is given:

Start-Process -FilePath 'C:\Program Files\Hello.exe' -NoNewWindow -Wait

Also, Start-Process allows you to pass additional arguments to the process:

Start-Process -FilePath 'C:\Program Files\Hello.exe' -ArgumentList 'Hello, world' -NoNewWindow -Wait

The cmdlet does not rotate anything for the promotion, but for the help of -PassThru you can instruct to rotate the object to the process. This object should be manually placed at the change:

$process = Start-Process -FilePath 'C:\Program Files\Hello.exe' -Wait -PassThru

so you can then you can recognize a lot of brown speeches, such as the status:

$process.HasExited

$process.ExitTime

or vikonannya code:

$process.ExitCode

.NET

In the .NET principle, the Process class can be accessed directly without the Start-Process cmdlet. For example, you can start the process with the command:

::Start('C:\Program Files\Hello.exe')

Such a way to do it is unhandy and bulky (in my opinion), ale trochs gnuchkishiy at vikoristanni. For example, let's run our program for a streaming window, pass arguments to it and take the result of the wink:

$process = New-Object -TypeName System.Diagnostics.Process
$process.StartInfo.FileName = C:Program Files Hello.exe
$process.StartInfo.Arguments = ″Hello,world″
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.UseShellExecute = $false
$process.Start()
$process.WaitForExit()

$process.StandatdOutput.ReadToEnd()

WMI

For the help of WMI, you can do almost everything, including launching the program. For what cycle method Create wmi-class Win32_Process. This method launches a process on a local remote computer via RPC. For example, for vikonannya programs on local computer you can speed it up with this command:

()″Win32_Process″).Create('C:\Program Files\Hello.exe')

And for viewing on a remote computer, the command looks like this:

()″\\remotecomputer\root\cimv2:Win32_Process″).Create(′C:\Program Files\Hello.exe′)

As an option, you can use the Invoke-WmiMethod cmdlet:

Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList ″C:Program FilesHello.exe″

Or with the Invoke-CimMethod cmdlet:

Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @(CommandLine=″C:\Program Files\Hello.exe″)

WMI starts the process in a short time and rotates the object to retrieve the process ID (ProcessID) and the result of the return (ReturnValue). ReturnValue can take the following values:

0 - Successful Completion
2 -Access Denied
3 - Insufficient Privilege
8 -Unknown Failure
9 - Path Not Found
21 - Invalid Parameter

Invoke-Command

The Invoke-Command cmdlet can invoke commands locally or remotely using WinRM. For example, to run the program on the local computer, you can use the command:

Invoke-Command-ScriptBlock (C:Program FilesHello.exe)

You can pass arguments to the program for consumption:

Invoke-Command -ScriptBlock (C: Program Files Hello.exe Hello, world)

Catch the respect that Invoke-Command can’t be friends with problems, that, in order to get pardons, has to be cunning with paws. Vtіm, such problems can be eliminated, for example, by combining a different cmdlet with the & operator:

Invoke-Command-ScriptBlock(& 'C:\Program Files\Hello.exe')

In general, Invoke-Command is zastosovuetsya for long-distance keruvane, but the main problem is the possibility of one-hour vykonanny on a few computers. For example:

Invoke-Command -ScriptBlock("C:\'Program Files'\Hello.exe") -ComputerName SRV1,SRV2,SRV3

$scriptblock = (C:Program FilesHello.exe)
$Computers = @('SRV1','SRV2','SRV3')
Invoke-Command -ScriptBlock $scriptblock -ComputerName $Computers

After the promotion, the cmdlet rotates the result of the program, and if you run it in the background (AsJob parameter), then it rotates the Job object:

Invoke-Command -ScriptBlock (C:\'Program Files'\Hello.exe) -ComputerName localhost -AsJob -JobName Hello

Invoke-Item

The Invoke-Item cmdlet assigns to an invoke to a standard file. So you can run the wicking file with the command:

Invoke-Item -Path ″C:Program FilesHello.exe″

However, it's best to use Invoke-Item to open single file types. For example, this is how we view a text file:

Invoke-Item -Path ″C:\Files\test.txt″

And so must text files in papacy:

Invoke-Item -Path ″C:\Files\*.txt″

cmd

Well, finally, one more way to launch programs from PowerShell for the help of the cmd shell. The way to do it is "indirect", but it's not less practical. The command is to start a new instance of cmd, run into the new assigned program, end the cmd robot and turn the result:

cmd /c ″C:Program FilesHello.exe″

Such an axis of anonymous ways to run the program is based on PoSh. I leather of them is good for singing situations.

Before the speech, the article was written for the motives of PowerShell: Deep Dive and Best Practice. I recommend reading there more richly cicago.

© 2022 androidas.ru - All about Android