What is PowerShell? How to open PowerShell, all methods What is Windows PowerShell

💖 Like it? Share the link with your friends
73095

And while poring over the Windows 7 start menu or the Windows 8 Start screen 1, novice users often discover applications that they don't understand. One such application is PowerShell. What is this very PowerShell and is there any sense from it? Well, since someone invented it and integrated it into Windows, it means that there is still some sense. By the way, Microsoft is the inventor of PowerShell.


So, . This application is intended primarily for system administrators, but ordinary users, especially beginners, will almost never benefit from it. PowerShell is a modern standardized command line shell that provides greater flexibility in managing a computer based on the Windows operating system. Essentially, PowerShell is the same command line but much more functional.

What can this administration tool do? Many things. With it, system administrators and advanced users can manage processes, services, file storage, accounts, and even servers. The shell also allows you to run external commands, work with objects .NET, COM And XML, do different tricks, etc., create and execute your own scenarios. By the way, Windows provides a special graphical console for writing scripts. ISE, which is a kind of addition to the main PowerShell shell.

Like the console cmd, PowerShell has its own set of commands called cmdlets. The utility recognizes many of the CMD commands, but in general, the syntax of the language, the definitions of cmdlets and their parameters differ from what users are used to seeing in a regular command line. Also, familiar commands like dir , cd , copy etc., well understood by PowerShell, are actually aliases. For example, the command Dir , used to browse the file system corresponds to the "native" cmdlet Get-ChildItem. Aliases, or as they say, aliases, are purely for convenience.

Understanding PowerShell for a novice user, especially if he is unfamiliar with at least the basics of a regular command line, will not be easy. And there are not so many guides and reference books that are clear enough and easy to understand. There is a good guide written by Frank Koch that we recommend reading if you still decide to start learning the PowerShell scripting language. And, of course, the shell's own help system.

It is powerful and deployed, with many specific examples. Using it is quite simple. Basic information about the help system is displayed by the cmdlet Get Help. The cmdlet lists all available commands.

PowerShell has a number of execution modes that determine what type of code is allowed to run. All of this is controlled by a registry key that lives in HKLM. There are 4 different execution modes:

Limited(Restricted): The default execution policy, does not allow scripting and only allows interactive commands.

All signed(All Signed): Allows all scripts to run. True, all scripts and configuration files must be signed by a publisher you trust; this mode exposes you to the risk of running signed (but malicious) scripts after you've received proof of trust from the publisher.

Remote signed(Remote Signed): Local scripts work without a signature. All downloaded scripts must be digitally signed.

Unlimited(Unrestricted): All scripts and configuration files obtained from communication applications such as Microsoft Outlook, Internet Explorer, Outlook Express and Windows Messenger work after confirming that you understand that the file comes from the Internet; no digital signatures are required; this mode exposes you to the risk of running unsigned, malicious scripts.

The default mode for PowerShell is "Restricted". In this mode, PowerShell only works as an interactive shell. It does not allow scripting, and only loads configuration files signed by a publisher you trust.

If you're getting an annoying red error, then in most cases it's because you're trying to run an unsigned script. The safest way to solve this problem is to change the execution policy to unrestricted, run the script, and then fall back to the restricted policy.

To change the execution policy to unrestricted, run the following command in administrative PowerShell:

Set-ExecutionPolicy Unrestricted

You will see a confirmation request. Press Enter.

Now you can run the downloaded script. However, you are putting yourself at serious risk, so when the script ends, don't forget to change the execution policy back to restricted mode. You can do this with the following command:

Set-ExecutionPolicy Restricted

And again you will see a confirmation request. Press Enter.

That's all! Good luck to you!

There are several different ways to run the script, below are the main ones:

  1. Start a PowerShell Shell and run the script in it (enter the path to the file and the file name, for example, like this: C:\Scripts\test.ps1, or go to the script folder with the cd C:\Scripts command and run it with the command.\test.ps1) The shell can be found and launched in a variety of ways. One way is through the Start menu. For Windows 7 - you need to go to "All Programs" - "Accessories" - "Windows PowerShell" and start the "Windows PowerShell" shell. For Windows 10 - you need to find the group with the letter "W" and in it you will find "Windows PowerShell".
  2. Launch "Windows PowerShell ISE"- this is a program - a development environment that allows you to edit and debug PowerShell scripts, which is also there by default along with PowerShell itself. Having launched it, you just need to click the "Open" button or go to the File menu - Open and select the desired script, then press F5 or the "Run Script" button. You can find Windows PowerShell ISE in the same place as the PowerShell shell - in the Start menu .
  3. Run standard command line and write in it:
    powershell-file<имя_скрипта> (example: powershell -file myscript.ps1)

If you have not run PowerShell scripts before, you will most likely receive a message: Can't upload file<имя_скрипта>because script execution is not allowed on this system. Type "get-help about_signing" for more information. This is due to the fact that for security reasons and to prevent accidental execution of malicious code, all scripts must be digitally signed.

How to allow execution of an unsigned PowerShell script?

1. In PowerShell, before running the script, execute a command that allows the execution of unsigned scripts for the current shell session:

Set-ExecutionPolicy RemoteSigned -Scope Process

2. Startup from the standard command line use the -executionpolicy option, i.e. run the script like this:

<имя_скрипта>

Both methods set the policy only for the current session, while the PowerShell script execution security policy specified in the registry will not be changed and will remain the same. If you want to change the script execution security policy to permanent, then use this method:

3. Allow launch forever: run PowerShell as "Administrator", then run the command:

Set-ExecutionPolicy RemoteSigned

Note: If the script was downloaded from the Internet, then in order to avoid a request to confirm the launch, you need to use Bypass instead of RemoteSigned - the complete disabling of any requests and warnings.

How to run a PowerShell script in the background?

To do this, just use the -WindowStyle launch parameter, which can take the following values: Normal, Minimized, Maximized and Hidden. Thus, to run an unsigned script in the background, you need to run the command:

powershell -executionpolicy RemoteSigned -WindowStyle Hidden -file<имя_скрипта>

You can also add -NonInteractive if you wish, so that the script does not ask any questions. Thus, the script will be executed imperceptibly for the user. Be careful using this method.

Running a PowerShell script with parameters

Actually, the launch should be done as if you were launching a regular program or a bat file with parameters. For example, to run a script with parameters from the command line, you can write the following command:

powershell -executionpolicy RemoteSigned -file<имя_скрипта>param1 param2 "another text parameter"

In the script itself, you can get these parameters like this:

Param ($var1, $var2, $var3) echo $var1, $var2, $var3

In the PowerShell ISE, you can run a script with parameters in the same way using the command pane.

How to run a PowerShell script using a shortcut?

There are two ways to accomplish this task:

  1. Create a bat / cmd file in which to register a command to run the script (you have read the parameters above)
  2. Create a shortcut to PowerShell, which can be found in c:\Windows\System32\WindowsPowerShell\v<версия>\ and in the properties of the shortcut in the field "Object" add the necessary parameters.

Thus, for example, to run a powershell script when a user logs in, you can simply create a shortcut, as described in the 2nd paragraph, and put it in autoload. Also, creating a shortcut using one of the methods above will make it easy to run the script as an administrator or as any other user as a regular program.

The PowerShell scripting language is a pretty powerful tool for solving various problems, but it can be used not only for good things, but also for harm, so use it wisely;)

In this article, we will consider such a technology from Microsoft as Windows PowerShell, we will talk about what PowerShell is, what cmdlets and the pipeline are, how to write scripts and modules, and also touch on other equally important and useful features of Windows PowerShell.

Before the advent of PowerShell, there were ( and exist) the following tools for automating and administering services: Windows command line and Windows Script Host. But these technologies have drawbacks.

The Windows command line has both the ability to perform some administrative tasks and the ability to write an action algorithm, saving it as a script (bat-file), while you can use some programming elements, for example, use variables, conditional structures, and even something like cycles.

Most software products have a console interface, i. we can control the program using the command line, while saving resources due to the lack of costs for the graphical interface. Microsoft for the server operating system Windows Server even releases editions without a graphical interface ( Server Core, Windows Server 2016 even has Nano Server), but all this is not enough, since the possibilities of the command line are limited, i.e. we will not be able to write some complex logic to automate something, and if we can, then we will need time and knowledge for this.

Also, the disadvantage of the command line is that we use various console utilities of software products to fully manage the server, and therefore it is really difficult to use the command line for administration, in terms of the fact that all these utilities have different syntax, parameters, methods of use and in order to perform typical tasks, i.e. everyday, you have to keep it all in mind, and to perform non-standard tasks, you first have to read help or documentation.

Windows Script Host technology allows you to perform all administrative tasks that the command line does, including their automation by writing WSH scripts, but here we can already use full-fledged programming languages ​​( VBScript and JScript), i.e. you can implement complex logic and algorithms. In addition, with the help of WSH, we manage software products through an object interface, in other words, the Windows Script Host is much " cooler' than the command line. But this technology also did not become that ideal administration tool and automation of this administration for system administrators, since Windows Script Host required knowledge of the above programming languages, which is actually superfluous for system administrators. Administrators need only a simple administration tool with the ability to program some actions, and they do not want to delve into the object models of software products in the VBScript and JScript programming languages.

As a result, Microsoft needed to develop an administration tool for system administrators that would 100 percent satisfy all the needs of system administrators, both in terms of administration and automation capabilities, and in terms of convenience and ease of use, thus Windows PowerShell was born.

What is Windows PowerShell?

Windows PowerShell is a scripting language and Windows command shell designed to administer and configure Windows operating systems. PowerShell is based on the CRL and the .NET Framework, and unlike the command line that accepts and returns text, Windows PowerShell works with objects. Every object in PowerShell has properties and methods that you can use to manipulate those objects.

In Windows PowerShell, Microsoft developed the concept of cmdlets ( cmdlets), which is a command naming system " Verb Noun". This system allows system administrators to quickly master and simplify work with PowerShell.

With Windows PowerShell, you can:

  • Get access to the file system;
  • manage the registry;
  • manage services;
  • manage processes;
  • Set up the operating system;
  • Install software;
  • Install server roles and components;
  • Administer and configure server roles and components;
  • Write and use scripts to automate management and administration;
  • Perform other system administrator tasks.

Windows PowerShell contains many commonly used utilities and commands that can be run from the command line, such as ipconfig, ping, and others. This is done in order to facilitate the transition of system administrators from the command line to PowerShell.

Also, for convenience, many frequently used commands and utilities in PowerShell have synonyms ( Alias), for example, cls is a synonym for the Clear-Host cmdlet, dir is a synonym for Get-ChildItem ( the full list of synonyms can be viewed by running the Get-Alias ​​cmdlet).

To simplify the search for the desired command in PowerShell, there is a special cmdlet Get-Command, with which you can search both by verb and by noun. All commands in Windows PowerShell are grouped into modules ( e.g. Hyper-V, NetTCPIP), which also makes it easier to find the right command.

After the desired command is found, you can see the instructions for working with this command, i.e. help, there is a special Get-Help cmdlet for this purpose, for example, the following command will display help on the Get-Command cmdlet:

Get-Help Get-Command

Help in Windows PowerShell can be short, detailed ( parameter -Detailed), complete ( -Full option), and you can also display only examples ( parameter - Examples). The following command will only show examples of using the Get-Command cmdlet:

Get-Help Get-Command -Examples

PowerShell Help is updatable, i.e. it can be updated with the Update-Help command.

Versions of Windows PowerShell

The first version of PowerShell 1.0 appeared on November 14, 2006 and was released as a separate distribution that could be installed on the following versions of Windows operating systems: Windows XP Service Pack 2, Windows Server 2003 Service Pack 1, and Windows Vista.

In Windows Server 2008, PowerShell 1.0 was shipped as a component that also had to be installed.

Beginning with Windows 7 and Windows Server 2008 R2, PowerShell is shipped as an integral part of the system ( those. pre-installed, no need to install it). Below is a table of correspondence between the version of PowerShell and the version of the Windows operating system ( those. which version of PowerShell is installed by default on which 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

With each new version of PowerShell, it becomes more and more powerful administration tool, in comparison, the first PowerShell had about 130 cmdlets, and in PowerShell 5.0 there are already more than 600 of them!

You can find out the current version of PowerShell using the PSVersion property of the $PSVersionTable built-in variable, for example, run the following command:

$PSVersionTable.PSVersion

Or run the cmdlet

Get-Variable -Name PSVersionTable –ValueOnly

where, the PSVersion value will be the PowerShell version.

PowerShell Language

PowerShell is an object-oriented scripting programming language. It is used to write commands to control all components of the Windows operating system in Windows PowerShell, and to write scripts to automate administrative tasks in the Windows PowerShell Integrated Scripting Environment (ISE). Although the PowerShell language was created for administration tasks, it is a full-fledged scripting programming language, as it has programming constructs that are present in every programming language, such as: conditions, loops, error handling, working with variables, objects, arrays.

The PowerShell language has a unified syntax for writing commands and a naming structure for these commands according to the principle " Verb Noun”, which makes this language intuitive for both programmers and system administrators.

Windows PowerShell shell

Windows PowerShell shell is an execution environment for commands and scripts written in the PowerShell language. This shell has the same features as the command line, such as: storing the history of command execution, customizing the appearance of the shell, completing command execution with the Ctrl + C key combination, as well as many other features that are not in the command line shell, for example, such a wonderful feature as " syntax highlighting» ( appeared in PowerShell 5.0).

You can start a PowerShell shell in several ways, for example:

  • From the command line by typing PowerShell;
  • Through the dialog box Run» ( keyboard shortcut Win+R), also by typing PowerShell;
  • In Windows 7 - Start->All Programs ->Accessories ->Windows PowerShell ->Windows PowerShell;
  • In Windows 8.1 or Windows Server 2012 R2 - Start->All Programs->System Tools->Windows PowerShell;
  • On Windows 10 or Windows Server 2016 - Start->All Programs -> Windows PowerShell Directory ( in group W) -> Windows PowerShell.

Example of running PowerShell in Windows Server 2016

Screenshot of PowerShell in Windows Server 2016

Cmdlets in PowerShell

Cmdlet (cmdlet) is a Windows PowerShell command that allows you to interact with and manipulate operating system objects. These commands are part of the PowerShell language. Cmdlets are built on the principle of " Verb Noun”, separated by a hyphen (-); in other words, we first indicate what to do, and through a hyphen - over what. For example, the Get-Help cmdlet, where Get is a verb meaning " Get", and Help is a noun " Help"in the context of PowerShell" Show - Help". PowerShell cmdlets return results as objects, which is one of the main differences from the Windows command line, where commands return only text to the screen.

In addition to cmdlets for getting data (Get), there are also such types of cmdlets as:

  • Add - adding data;
  • Clear - clear;
  • Enable - enable;
  • Disable - turn off;
  • New - create;
  • Remove - remove;
  • Set - set;
  • Start - start;
  • Stop - stop;
  • Export - export;
  • Import - import;
  • And many more.

A complete list of cmdlets in Windows PowerShell can be viewed using the special Get-Command cmdlet. For example, run it with the -CommandType cmdlet parameter, as a result, you will see a list of cmdlets on the screen.

Get-Command -CommandType cmdlet

As you already understood, cmdlets have parameters with which we can specify the actions of the cmdlet. There are required and optional parameters, for example, the Get-Command cmdlet has no required parameters.

The picture below shows a way to search for a cmdlet by verb ( Verb parameter). In this case, we have displayed a list of cmdlets that can restart something.

Team

Get-Command -Verb Restart

To search for a cmdlet by noun, you must use the Noun parameter. For example, below we got a list of cmdlets that work with services.

Team

Get-Command -Noun Service

If you did not find the required cmdlet by its full name, you can use the mask in *Text* format.

Pipeline in PowerShell

One of the main features of Windows PowerShell is the ability to use a pipeline when executing commands.

Conveyor- this is the transfer of the result of the work of the cmdlet through the vertical bar (|) to another cmdlet. At the same time, as you remember, cmdlets in PowerShell work with objects and return objects, respectively, objects are also transferred along the pipeline.

Using a pipeline, you can perform complex tasks in a simple and convenient way without the need to write complex algorithms and scripts.

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

Team

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

  • Get-ChildItem - cmdlet for getting objects in the specified directory;
  • 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 desired properties of an object, in our case we display standard fields and only the very first object, i.e. big file ( parameter -First 1).

All cmdlets are separated from each other by a vertical bar. The result of each cmdlet is passed as input to another cmdlet, i.e. first we get all the objects in the specified directory, then we sort the result and finally select the very first object.

Background execution of jobs

Windows PowerShell has the ability to run jobs in the background, it is a mechanism by which you can run a command ( for example, which takes a long time to complete) in the background, i.e. after launch, you return to the current session and can continue working without waiting for the command to finish. You will need this feature when you need to run a command that takes a long time, and as you know, during this time the PowerShell shell session is blocked until the command completes, and you need to continue working.

You can manage all tasks that are running in the background, for example, view the list of tasks, stop the task, delete the task, and, of course, view the result of the task.

Windows PowerShell provides the following cmdlets for working with background jobs:

  • Start-Job - start a background task;
  • Stop-Job - stop a background task
  • Get-Job - view a list of background tasks;
  • Receive-Job - view the result of the background task;
  • Remove-Job - remove a background task;
  • Wait-Job - bring a background task to the foreground in order to wait for it to finish.

To run in the background, you must write the command Start-Job, and in curly brackets () the command or set of commands that you want to run in the background.

For example, let's run some task ( show list of services) in the background, then look at the list of background jobs and display the result of our job ( those. service list).

Running a task in the background

Start-Job (Get-Service)

We look at the list of tasks running in the background

Displaying the result of the job Job1

Receive Job Job1

As you can see, we have a task with the status " Completed”, i.e. it has already been completed just Get-Service works fast).

In order to view the result of the background task, i.e. Get-Service cmdlet, we executed the Receive-Job command and passed the name of the job to it ( can and id value). As a result, we have displayed a list of services.

Remote control with PowerShell

Windows PowerShell is designed not only for local use, but also for remote command execution. This feature is necessary so that you can manage remote computers from your workplace, i.e. run PowerShell commands.

There are several ways to remote control:

  • Using the parameter –ComputerName (many teams have). In other words, you pass the name of the computer on which you want to run the command as a parameter. The method has the disadvantage that it is limited to the execution of a single command;
  • Through sessions. Cmdlet Enter-PSSession (interactive session). In this way, you connect to a remote computer and all the commands that you type in the PowerShell shell will be executed on the remote computer in the same way as if you typed commands directly on the remote computer. The method also has the disadvantage that the session is limited to one computer;
  • Using the cmdlet Invoke-Command. Using this method, you can run commands or scripts on one computer or on several.

For example, to connect to a remote computer ( in the example below ServerName) interactive session, run the following command:

Enter-PSSession ServerName

Scripts, Functions, and Modules in Windows PowerShell

If you have written some algorithm, most likely you will want to save it in order to use it in the future. Windows PowerShell has a scripting mechanism for this purpose.

The essence of this mechanism is as follows, you write the set of commands you need and save it as a script - this is a simple text file with the .PS1 extension.

Then, to run this script, you simply run it in PowerShell. In this case, you must either specify the full path to the script file, or go to the directory with the script and call it by name.

Important!

By default, scripting is disabled on Windows! To view the script execution policy, run the cmdlet Get-ExecutionPolicy. As a result, it will return the current policy, for example:

  • Restricted - blocking the execution of any scripts ( default value);
  • AllSigned - execution of scripts that have a digital signature is allowed;
  • RemoteSigned - execution of local scripts is allowed, all downloaded scripts must be digitally signed;
  • Unrestricted - execution of any scripts is allowed ( not recommended as unsafe!).

You must use the cmdlet to allow scripts to run. Set-ExecutionPolicy with one of the above options.

For example, to allow the execution of local scripts, run the following command, and agree to the changes by pressing Y.

Set-ExecutionPolicy RemoteSigned

In a script, you can pass parameters, make them required, or set a default value.

Windows PowerShell provides a mechanism to create your own functions, which, like the built-in cmdlets, can be used in the PowerShell shell.

To do this, you need to specify the keyword Function and then in curly brackets () write the algorithm for this function, i.e. instruction set ( for example, some commonly used procedure: create a user with certain rights, clean up certain directories, and so on). Then you need to save all this into a script, but only with the .psm1 extension, since this file will already be a module.

That's not all, this file needs to be placed in the directory where PowerShell looks for modules. There are several directories a special directory in the user profile, the directory where PowerShell is installed), they can be viewed in the PowerShell environment variables. To do this, run the following command

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

Once you place the module you have written in a special directory, you can access your functions like normal PowerShell commands.

Windows PowerShell Integrated Scripting Environment (ISE)

In order to make it convenient to write scripts, functions and, accordingly, modules, Microsoft has developed a special graphic program Integrated Scripting Environment(ISE) - Integrated Scripting Environment. It is very convenient to work in this program, besides, it has powerful functionality ( creation of many tabs with scripts, output area, built-in debugger and more).

You can run it like this:

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

Note! ISE will not run on a Windows Server system installed in the Server Core variant.

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

That's all I have, I hope the material was useful to you! Good luck!



tell friends