How to open PowerShell, all methods. Windows PowerShell Basics What is windows powershell ise

Automation only. PowerShell only.

Preface

As a hobby and when I have time, I teach students at UKIT (formerly Moscow State College of Information Technologies). At the moment, I have little time to devote to a group of students, but it is enough to prepare a post here on Habré.

I work as a system administrator in a large non-IT company with a lot of IT resources. By type of activity, it seems to be solving a large number of similar tasks for servicing users.

I became acquainted with the PowerShell language about two years ago, but took up it in earnest only a year later, not realizing at first its enormous capabilities. In this article, first of all, I will focus on those who want to start working with PowerShell, but do not yet trust it or do not know which way to approach this miracle.

Warning: PowerShell is addictive.

Introduction

Wikipedia tells us:
Windows PowerShell- extensible automation tool from Microsoft, consisting of a command-line shell and an accompanying scripting language.

The PowerShell environment may look like the command line:


powershell.exe

Or as an application:


powershell_ise.exe

Powershell_ise.exe called the integrated scripting environment - Windows PowerShell ISE. Allows you to work with the language in a convenient environment with syntax highlighting, a command constructor, auto-completion of commands by pressing TAB and other amenities. Ideal for creating and testing scripts.

To start the environment powershell.exe or powershell_ise.exe Just type the same name in the execute line.

The PowerShell script file has the extension .ps1.

The script cannot be launched with a double LMB. This is done specifically so as not to harm the system by an accidentally launched script.

To run, right-click and select “Run using PowerShell”:

In addition to the fact that there is a restriction on running LMB scripts, by default executing scripts in the system is prohibited, again, for the reason described above - not to harm the system. To check the current execution policy, run the command:

Get-ExecutionPolicy

We will get one of the following values. With a high probability, if this was the first launch, we will get Restricted.

  • Restricted- Scripts cannot be run;
  • AllSigned- Only scripts signed by a trusted publisher can be run. Confirmation will be requested before running the trusted publisher script;
  • RemoteSigned- Allowed to run scripts created by us and scripts downloaded signed by a trusted publisher;
  • Unrestricted- No restrictions, all scripts can be run.

For execution and testing, lower the policy to RemoteSigned by running the command:

Set-ExecutionPolicy RemoteSigned

Let's get started

Cmdlet
  • Cmdlets are PowerShell commands that contain various functionality;
  • Cmdlets can be either system or user-created;
  • Cmdlets are named according to the Verb-Noun rule, which makes them easier to remember;
  • Cmdlets output results as objects or collections of objects;
  • Cmdlets can both receive data for processing and transmit data through a pipeline (more about pipelines later);
  • Cmdlets are not case sensitive (you can write get-process, Get-Process, and GeT-pRoCeSs);
  • It is not necessary to put " after cmdlets ; ", except when we execute multiple cmdlets on one line (Get-Process; Get-Services).

For example, to get the current processes, we will run the command:

And we get the result:

Try it yourself:

Get-Service #to get the status of services running on computers

Get-Content C:\Windows\System32\drivers\etc\hosts #to get the contents of a file. In this case, the hosts file

You don't need to know all the cmdlets by heart. Get-Help will save the situation.
You can get information about all available cmdlets by running the following command:

Get-Help -Category cmdlet

If we use PowerShell ISE, we make the development process easier.
Just enter a dash " - " after we have entered the cmdlet, and we will get all possible options for parameters and their types:

Try:

Get-Service -Name p*

If, however, we forget what properties this or that cmdlet has, we’ll run it through Get-Member:

Get-Process | Get-Member #Sign "|" called a conveyor. About him below.

Not enough information? Let's refer to the help with the parameter -Examples:

Get-Help Get-Process -Examples

We get a description Get-Process, and even with examples of use:

  • Cmdlets can have abbreviated names - aliases. For example, instead of Get-Help can be used simply Help. To get all the abbreviations, run Get-Alias.

Try:

Start-Process notepad

Which is similar to writing:

Start notepad

Now let's stop the process:

Stop-Process -Name notepad

Spps -Name notepad

A little earlier we said that cmdlets are named according to the Verb-Noun rule. Let me clarify that the verb does not have to be Get. In addition to what we can receive, we can ask Set(remember, Set-ExecutionPolicy), run Start, stop Stop, output Out, create New and many others. The name of the cmdlet is not limited to anything, and when we create our own, we can name it whatever our heart desires.

Let's try to output to a file:

"Hello, Habr!" | Out-File C:\test.txt & C:\test.txt

By the way, you can write it like this:

"Hello, Habr!" > C:\test.txt & C:\test.txt

PowerShell is an object-oriented software engine and scripting language with a command line interface that provides IT professionals with greater opportunities for configuring operating systems of the MS Windows family. Simply put, it is a kind of universal administration tool. This article will discuss basic techniques for writing scripts in PowerShell, which allow you to automate the management of your Windows environment in a simple way.

PowerShell offers both a pure console interface and a full development environment PowerShell ISE(Integrated Scripting Environment, built-in scripting environment) for scripts. To launch the command line interface, type powershell in the Run menu (WinKey + R). PowerShell ISE is launched using the "PowerShell ISE" command in the same menu.

ISE is more preferable as it provides more opportunities to the developer thanks to syntax highlighting, code auto-completion and other features inherent in many “big” IDEs.

Writing and running scripts

Scripts are saved as files with the extension .ps1. Even though PowerShell has long been a native part of the Windows OS, you can't run its scripts with a simple double-click. To do this, right-click on the script and select “Run in PowerShell”.

There are also system policies that restrict script execution. You can check your current policy settings by running the Get-ExecutionPolicy command. The result will be one of the following values:

  • Restricted- execution of scripts is prohibited. Standard configuration;
  • AllSigned- you can run scripts signed by a trusted developer; Before running the script, PowerShell will ask you for confirmation;
  • RemoteSigned- you can run your own scripts or those signed by a trusted developer;
  • Unrestricted- you can run any scripts.

To get started, you need to change the startup policy setting to RemoteSigned using the Set-ExecutionPolicy command:

Cmdlets

Cmdlets are commands with a predefined function, similar to conditional statements in programming languages. They have several key features:

  • There are system, user and optional cmdlets;
  • the result of executing the cmdlet will be an object or an array of objects;
  • Cmdlets can process data and pass it to other cmdlets using pipelines;
  • cmdlets are case insensitive, so there is no difference between Get-ADUser , get-aduser , and gEt-AdUsEr ;
  • a symbol is used as a separator; .

Each cmdlet contains a verb and a noun separated by a hyphen. For example:

  • Get-Process- display current processes running on the computer;
  • Get-Service- display a list of services and their status;
  • Get-Content- display the contents of the specified file, for example Get-Content C:\Windows\System32\drivers\etc\hosts .

If necessary, you can list all available cmdlets using Get-Help-Category:

You can also create your own cmdlets.

Options

Each cmdlet has several parameters that determine how it works. PowerShell ISE automatically suggests all available parameters and displays their type. For example, Get-Service-NameW* returns a list of services whose name begins with W . If you forget what parameters the cmdlet you entered has, use Get-Member . For example, Get-Process | Get-Member:

If you don't find what you need, or aren't sure how to set the parameters correctly, you can even request examples using the -Examples parameter:

Some cmdlets can also be called using aliases, for example, instead of Get-Help you can simply write Help .

When writing large scripts or team development, you can use comments. Each comment begins with a # character, and the comment block is limited to combinations of characters<# и #>at the beginning and at the end respectively.

Conveyor

PowerShell Allows you to exchange data between cmdlets using a pipeline. For example:

  • GetService | SortObject -property Status - sorting running services by status;
  • “Hello World!” | Out-File C:\ps\test.txt - writing text to a file.

Multiple conveyors can be used. For example, the following script lists the names of all services except stopped ones:

Get-Service | WHERE ($_.status -eq “Running”) | SELECT displayname

Conclusion

So, thanks to this tutorial, newbies have an idea of ​​what PowerShell is all about. We also looked at options for changing the script execution policy, what a cmdlet is, how they exchange data using a pipeline, and how to get the properties of the desired object. Remember that if you have any problems, you can use the Get-Help cmdlet.

Experienced users of the Windows 10 operating system may have heard of the PowerShell command line utility. Those with experience working with Windows 7 and 8.1 may also be familiar with it. After years of using the Windows command line application and .bat files, it's time to get acquainted with a more powerful tool.

PowerShell is a valuable addition to the list of Windows tools, and its scope may be intimidating for some users. What is it - a scripting language or a command shell? Don't worry: despite the extensive capabilities, anyone can master PowerShell.

Step 1: Installation

First, we need the PowerShell utility itself. If you're running Windows 10, you already have version 5 of PowerShell installed. The Windows 10 Anniversary Update uses version 5.1, but the difference is not noticeable. Windows 8 and 8.1 use PowerShell 4, which is also quite sufficient. Installing PowerShell on Windows 7 will not cause problems, but you will still have to take a couple of extra steps. In particular, you need to additionally install the .Net Framework. You can install the Windows Management Framework, which includes PowerShell.

PowerShell has two interfaces. Advanced users can opt for a full GUI interface known as the Integrated Scripting Environment (ISE). For beginners, it's best to use the PowerShell console, a simple text-based Windows command line-style interface, or even DOS 3.2.

To run PowerShell as an administrator in Windows 10, click the Start button and scroll down to Windows PowerShell. On Windows 8.1, look for Windows PowerShell in the System folder in Windows. In Windows 7, the shell is located in the Accessories folder. As a regular user, PowerShell can be launched in the same way, using the left mouse button instead of the right one.

You can use search on any version of Windows. For ease of future use, you can pin PowerShell to the taskbar.

Step 2: Old Windows Commands

The Windows command syntax in PowerShell works as usual. For example, CD changes folders, dir shows a list of all files and folders inside the current folder.

Depending on how you launch the PowerShell console, you might start in c:\Windows\system32 or in c :\Users\ . The example in the screenshot uses the command cd..(with a space) to move up one level at a time, then run the command dir to display a list of files and folders in a directory C:\.

Step 3: Install Help Files

Teams like CD And dir are not active PowerShell commands. These are so-called aliases - replacements for real PowerShell commands. Aliases are useful for those who have extensive experience with the command line. However, they don't touch the depths of PowerShell.

To get started with PowerShell, type help and the command you need. The screenshot shows the command .

Team help says that dir is an alias for the PowerShell command Get-ChildItem. If you type get-childitem V PS C:\>, you will see the same thing as when using the command dir.

As noted at the bottom of the screenshot, help files for PowerShell are not installed automatically. To get them, run PowerShell as administrator and type update-help. Installing the help files will take several minutes; a number of modules may be missing - for example, in this case, Help for NetWNV and SecureBoot were not installed. When everything is ready, the complete help system will always provide the necessary tips.

Now type the command get-help and any command you are interested in (“cmdlet” in PowerShell, cmdlets in Russian), its description will be shown. For example, get-help get-childitem displays a list of options get-childitem. You can also display different possible options. For example

get-help get-childitem -examples

provides seven detailed usage examples get-childitem. Team

get-help get-childitem -detailed

includes these seven examples and detailed explanations of each parameter in the cmdlet get-childitem.

Step 4: Get help with settings

In the screenshot you may have noticed two lists under SYNTAX For get-childitem. Having two different syntaxes means there are two ways to run the cmdlet. How to keep the syntax separate from each other and what do the parameters mean? The answer is simple if you know the trick.

For details regarding cmdlet parameters get-childitem or any other use parameter -full:

get-help get-childitem -full

This gives you a list of what you can do with the cmdlet and what will happen. Look at the screenshot.

Reviewing the parameter descriptions, you will notice that get-childitem makes it possible to get an object child(such as a subfolder name or file name) in a specified location, with or without matching certain characters. For example:

get-childItem “*.txt” -recurse

returns a list of "*.txt" files in the current folder and all subfolders (due to the parameter -recurse). Whereas

get-childitem “HKLM:\Software”

returns a list of all top-level registry keys in HKEY_LOCAL_MACHINE\Software.

If you've ever tried to get into the registry using the Windows command line or .bat files, you'll appreciate the functionality of this access option.

Step 5: Learning Names

There's a reason why the cmdlets shown so far look similar: get-childitem, update-help, get-help use a single verb-noun pattern. All PowerShell cmdlets use this convention; they use a verb before a single noun. This will appeal to those who once suffered from inconsistent command names in the VB and VBA languages.

Take a look at the most common cmdlets:

set-location: sets the current working location to a specific location

get-content: Gets the contents of the file

get-item: Retrieves files and folders

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

remove-item: Deletes files and folders

: Gets processes running on a local or remote computer

get-service: Gets services running on a local or remote computer

invoke-webrequest: Retrieves content from a web page on the Internet

To view how a specific cmdlet works, use get-help as in the case

get-help copy-item -full

Based on the description in the help, you can understand what the cmdlet needs. For example, if you want to copy all files and folders from Documents V c:\temp, use

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

By typing this command, you'll see several interesting features of the PowerShell environment. For example, if you type copy-i and press the Tab button, PowerShell will fill in Copy-Item. If you type a cmdlet incorrectly and PowerShell cannot recognize it, a full description of what was done wrong is given.

Try this cmdlet:

invoke-webrequest askwoody.com

You'll get a short list of the web page's titles, images, links, and other content. Please note in get-help to the list invoke-webrequest, which “returns a collection of forms, links, images, and other important HTML elements”—exactly what should be shown on the screen.

Some cmdlets help you manage PowerShell itself:

get-command: list of all available cmdlets

get-verb: list of all available verbs

clear-host: Clear the host program screen

Different parameters allow you to reduce commands and narrow down the range of useful options. For example, to see a list of all cmdlets that work with Windows services, type

get-command *-service

All verbs available with the noun will be shown service. Here is their list:

Get-Service

New-Service

Restart-Service

Resume-Service

Set-Service

Start-Service

Stop-Service

Suspend-Service

You can combine these cmdlets with others.

Step 6: Using Pipes

If you are familiar with the Windows command line or batch files, then you know about redirection and pipes. Redirection (> symbol) and pipes (| symbol) take the result of an action and attach it to another location. For example, you can redirect the result of the command dir to a text file or pass the result of the command ping to the team find to filter interesting results like

dir > temp.txt

ping askwoody.com | find “packets” > temp2.txt

Here on the second team find searches for a string packets, taken from the address askwoody.com by the team ping and concatenates all matching lines into a file called temp2.txt.

The first of these commands works fine in PowerShell. To run the second command you would need something like

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

Using redirection and pipes greatly expands the capabilities of the Windows command line: instead of endlessly scrolling down the screen to search for a text string, you can filter the Windows commands you need.

Powershell has support pipe, and it is not limited to text. PowerShell allows you to pass an entire object from one cmdlet to another, where the object is a combination of data (called properties) and actions (methods) that can use that data.

The tricky part comes when lining up the objects. The objects supplied by one cmdlet must match the type of objects accepted by the receiving cmdlet. Text is a very simple object type, so if you're working with text, aligning objects is a simple task. The remaining objects are not so basic.

How to understand this? Use cmdlet get-member. If you want to know what type of object a cmdlet is processing, run it through get-member. For example, if you are trying to understand the processes running on a computer and have narrowed your options down to cmdlets , here's how to find out the result of the cmdlets:

get-process | get-member

Running this cmdlet produces a long list of properties and methods for , but at the very beginning of the list you can see the type of object that creates :

TypeName: System.Diagnostics.Process

The below screenshot also shows the properties entitled get-process Handles, Name, NPM, PM, SI, VM And W.S..

If you want to manipulate the result To work with this cmdlet (instead of displaying a long list of active processes on the monitor), you need to find another command that takes as input System.Diagnostics.Process. To find the cmdlet you need, use PowerShell again:

get-command -Parametertype System.Diagnostics.Process

This cmdlet provides a list of cmdlets that can process System.Diagnostics.Process.

Some cmdlets are known for accepting almost any kind of data. Chief among them is . This cmdlet passes through each object sent through the pipe, one by one, and applies the specified selection criteria to it. There is a special marker called $_ , which allows you to use each item in the pipe, one at a time.

Let's say you want to get a list of all processes running on a computer with the name "svchost", that is, you want to match the property Name process svchost. Use the command:

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

Cmdlet looks at every object System.Diagnostics.Process, compares .Name this object with "svchost"; if there are matches, they are displayed on the monitor. Look at the screenshot.

Step 7: Analyze Useful PowerShell Commands

By now you already know enough to be able to damage the computer, so be careful. Let's look, for example, at frequently requested PowerShell commands.

These commands only work on Windows 10 and only when you run PowerShell as an administrator. They are designed to reinstall pre-installed Windows 10 applications and can be useful for those who first uninstalled these programs and then decided to return them. The commands look like this:

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

If you run this command, ignore the red warnings and when the command completes, restart your computer; all pre-installed programs in Windows 10 will appear in their places.

Here's how this command works. Get-AppXPackage checks all application packages in the user profile. Even if you delete the application, it remains in the user profile list.

Cmdlet Get-AppXPackage returns an object TypeName Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage, which includes the fully qualified name of the application package and the location of the corresponding XML manifest file. If you run the cmdlet get-appxpackage, you will see a long list of application packages. The screenshot shows the description of the Xbox app.

Cmdlet Foreach loops through each object in AppXPackage by sending them to the cmdlet Add-AppxPackage. According to get-help For Add-AppxPackage, there are two key switches:

  • Switch -Register used to register existing installations of application packages, you can set parameters DisableDevelopmentMode And Register
  • Switch -DisableDevelopmentMode tells Windows to re-register an existing application package that has been disabled, unregistered, or corrupted.

Line " $($_.InstallLocation)\AppXManifest.x ml" describes where the file is located manifest.xml. If you look at the files AppXManifest.xml, you'll see a complex list of application IDs, executable files, and a large number of visual elements associated with the application.

After the reboot, all added application packages are downloaded and installed from the Windows Store.

After switching to the Windows 10 operating system, many users noticed the “Run PowerShell” button in the Explorer interface. In this regard, many questions appeared on the Internet, users were interested in what kind of Windows PowerShell program it was and what it was needed for. If you are also interested in this issue, then this material should help you.

Windows PowerShell is a scripting language and command-line program for executing these scripts. This language was released in 2006 as part of the second service pack for Windows XP and since then PowerShell has been part of all Microsoft operating systems. In 2008, the second version of this language appeared, and starting from Windows 7 it is this version that is used. Files with Windows PowerShell scripts have a PS1 extension and can be run as familiar BAT and CMD files.

Windows PowerShell is a scripting language designed primarily for Microsoft business customers who need powerful tools to automate the tasks of managing Windows-based servers and computers. The .NET platform, developed by Microsoft since 2002, was used as the basis for this language.

Windows PowerShell allows you to automate a wide variety of tasks. With it you can manage files, services, processes, accounts and settings. The PowerShell language understands many of the commands found in the regular Windows Command Prompt (CMD), but it also has its own language consisting of its own commands, which are called cmdlets.

Cmdlets (in English cmdlets) are formed according to the Verb-Noun rule, for example, Get-Help. You can learn the basics of Windows PowerShell using Help (the Get-Help cmdlet). For general information, simply enter the command “Get-Help”; for help about a specific cmdlet, enter “Get-Help Cmdlet-name”. For example, if we enter the command “Get-Help Get-Process”, we will get help about the Get-Process cmdlet.

How to run a Windows PowerShell program

The Windows PowerShell program can be launched in different ways. The easiest option is to simply use the search in the Start menu. To do this, open the Start menu, enter the search query “Windows PowerShell”, and then open the found program.

You can also use the Windows key combination-R and enter the command “powershell”.

In Windows 10, PowerShell can be launched from any folder. To do this, click on the "File" button in the upper left corner of Explorer and select "Run Windows PowerShell."

How to use Windows PowerShell

As we have already said, using the PowerShell program you can automate many routine tasks in the Windows operating system. As an example, we will demonstrate some of the capabilities that this tool has to make it clear how to use it.

Emptying the Trash. There is a special cmdlet for clearing the recycle bin called “Clear-RecycleBin”. This cmdlet is useful when writing scripts for servicing computers. When you run the cmdlet, you must specify the drive on which you want to empty the Recycle Bin. Example:

Clear-RecycleBin c:

The screenshot below shows how this cmdlet is executed. After entering the command “Clear-RecycleBin c:” a confirmation request appears. In order to continue executing the cmdlet, enter the letter “Y” and press Enter.

If you add “-Force” to the “Clear-RecycleBin c:” cmdlet, the recycle bin will be cleared without asking for confirmation. Screenshot below.

Archiving files. For archiving and unpacking files, the cmdlets “Compress-Archive" and "Expand-Archive" are provided. For example, in order to archive the folder “C:\test\” into the archive “C:\archive\test2.zip” you need to run the following cmdlet:

Compress-Archive -LiteralPath C:\test\ -DestinationPath C:\archive\test2.zip

As a result of executing the command shown above, a file “test2.zip” will appear in the “C:\archive\” folder, in which the contents of the “C:\test\” folder will be archived.

List of cmdlets for Windows PowerShell

There are a huge number of different cmdlets for Windows PowerShell and it will not be possible to describe them all in one article. Therefore, we suggest that you familiarize yourself with the most popular of them:

  • Get-Help – getting help;
  • Update-Help – update help;
  • Get-Command – search for cmdlets;
  • Get-Alias ​​– search for aliases for cmdlets;
  • Get-PSDrive – view connected drives;
  • Get-Member – view the properties and methods that an object has;
  • Get-WindowsFeature – view information about available server roles and features;
  • Install-WindowsFeature (analogous to Add-WindowsFeature) - installs roles or components on the desired server;
  • Uninstall-WindowsFeature (analogous to Remove-WindowsFeature) – removes server roles or components;
  • Get-History – viewing the history of commands that were entered in this session;
  • Get-Variable – view a list of variables and their values;
  • New-Variable – creating a new variable;
  • Set-Variable – setting the value of a variable;
  • Clear-Variable – deleting a variable value;
  • Remove-Variable – removing a variable and its value;
  • Format-List – viewing the result of the command as a list of properties, where each line contains a separate property;
  • Format-Table — view the command result in table format;
  • Format-Wide - view the result of the command in the form of a wide table, which shows only one property for each object;
  • Format-Custom – view the result of the command using a custom view;
  • Export-Csv – export data to CSV format;
  • Import-Csv – import data from a CSV file;
  • Export-Clixml - export data to XML format;

You can get a complete list of cmdlets for Windows PowerShell by running the Get-Command -CommandType cmdlet.

Users who are in the process of learning the Windows 8.1 or Windows 7 Start screen often launch applications that are completely unfamiliar to them. One of these is Power Shell, a product from Microsoft.

What is Power Shell

If someone worked hard to write the code for a product, it means that it is needed by someone, intended for someone. Most likely, an ordinary user will be able to derive minimal benefit from the presence of this software, but for a system administrator, this program is an excellent assistant.

Power Shell is a modern, standardized command line shell that provides access to more flexible management of a Windows-based computer. Essentially the same command line, but the possibilities are much wider.

Administration tool functionality

The functionality of the software is impressive. With it you can manage services, accounts, file storage, processes and even servers. The shell provides access to working with COM, NET, XML objects, running external commands, creating and executing your own scripts, and performing other operations.

The Power Shell has its own set of commands, just like the CMD, called cmdlets. The syntax of the language, definitions of cmdlets and their parameters in this program differs from the usual, although the utility is able to recognize many CMD commands.

Well-understood Power Shell commands like cd, dir, copy and the like in this shell are aliases or, as they say, aliases, serving simply for convenience. Example: The dir command entered by the user to view the file system corresponds to the Get-ChildItem cmdlet.

How to understand the program

It will be difficult for a person unfamiliar with the basics of the command line to understand the intricacies of working with the utility. Unfortunately, there are few simple and understandable reference books on this topic.

When deciding to start learning the Power Shell scripting language, it is advisable to read the manual created by Frank Koch, or refer to the help system of the program itself. The latter is quite powerful, with lots of concrete examples. The basic information here will be available for viewing after you enter the Get-Help cmdlet.