Only automation. PowerShell only.

Foreword
As a hobby and given time, I teach students at UKIT (the former Moscow State College of Information Technology). At the moment I have little time to devote him 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 large focus on IT resources. By the nature of the activity, it seems to solve a large number of similar tasks in user services.
I became acquainted with PowerShell about two years ago, but I came to grips with it only a year later, not realizing at first its enormous possibilities. In the article, first of all, I will focus on those who want to start working with PowerShell, but do not trust it yet or don’t know from which side to approach this miracle.
Warning: PowerShell is addictive.Introduction
Wikipedia tells us:
Windows PowerShell is an extensible automation tool from Microsoft , consisting of a shell with a command line interface and a related scripting language.
PowerShell can look like the command line:
powershell.exeOr in the form of an application:
powershell_ise.exePowershell_ise.exe is called the integrated script environment - Windows PowerShell ISE. Allows you to work with the language in a convenient environment with syntax highlighting, command constructor, autocomplete commands by pressing TAB and other delights. Ideal for creating and testing scripts.
To launch the
powershell.exe or
powershell_ise.exe environment, just type the same name in the line to execute.

The PowerShell script file has the extension
.ps1 .

The script will not work to run a double paint. This is done specifically in order not to harm the system by accidentally running a script.
To start, click on the RMB should choose "Run with PowerShell":

In addition to the fact that there is a restriction on the launch of LKM scripts, by default execution of scripts in the system is prohibited, again, for the reason described above, do not 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. Before executing the trusted publisher script, confirmation will be requested;
- RemoteSigned - Allowed to execute scripts created by us and downloaded scripts signed by a trusted publisher;
- Unrestricted - No restrictions, all scripts can be run.
For execution and testing, we will lower the policy to
RemoteSigned by running the command:
Set-ExecutionPolicy RemoteSigned

Getting Started
Cmdlet
- Cmdlets are PowerShell commands that have different functionality;
- Cmdlets can be either system or user-created;
- The cmdlets are named by the Verb-Noun rule, which makes memorization easier;
- The cmdlets display the results in the form of objects or their collections;
- Cmdlets can both receive data for processing and transmit data through the pipeline (about pipelines later);
- The cmdlets are not case sensitive (you can write get-process, and Get-Process, and GeT-pRoCeSs);
- After the cmdlets do not necessarily put " ; ", except when we run several cmdlets on one line (Get-Process; Get-Services).
For example, to get the current processes, we run the command:
Get-Process
And we get the result:

Try it yourself to do:
Get-Service # ,
Get-Content C:\Windows\System32\drivers\etc\hosts # . , hosts
It’s not necessary to know all the cmdlets by heart.
Get-Help will save the situation.
Information on all available cmdlets can be obtained by typing the following command:
Get-Help -Category cmdlet
If we use PowerShell ISE, we facilitate the development process.
Just enter the dash "
- " after typing the cmdlet, and we will get all the possible options for the parameters and their types:

Try this:
Get-Service -Name p*
If, nevertheless, we forget what properties a particular cmdlet has, run it through
Get-Member :
Get-Process | Get-Member # "|" . .

Not enough information? Refer to the help with the
-Examples option :
Get-Help Get-Process -Examples
We
get a description of
Get-Process , and even with examples of use:

- Cmdlets can have abbreviated names - aliases. For example, instead of Get-Help, you can simply use Help . To get all the abbreviations, do Get-Alias .
Try this:
Start-Process notepad
What is similar to the recording:
start notepad
Now stop the process:
Stop-Process -Name notepad
Or so:
spps -Name notepad
A little earlier, we said that cmdlets are referred to as Verb-Noun. Clarify that the verb does not have to be
Get . In addition to what we can receive, we can set the
Set (remember, Set-ExecutionPolicy), start
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 with you, we can name it as it pleases.
Let's try to output to the file:
"Hello, Habr!" | Out-File C:\test.txt & C:\test.txt
By the way, you can write the same way:
"Hello, Habr!" > C:\test.txt & C:\test.txt
Comments
We all know to use comments is good form.
Comments in PowerShell are lowercase -
# and block -
<# ...
#> :

Let's pay attention to the code from the example:
Get-WmiObject -Class Win32_OperatingSystem | SELECT Caption
For those who are familiar with
WMI , who do it on good old VBScript, remember how much code you need to write?
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48) For Each objItem in colItems Wscript.Echo "Caption: " & objItem.Caption Next
Conveyor
Pipeline (
| ) - transmits the output of one command to the input data for processing by another command. We used the pipeline earlier, getting all the properties of the object or, in the previous example, choosing from the data set only the Caption field.
To understand the principle of the pipeline, let's run the code:
Get-Service | Sort-Object -property Status
What happens: we get all the services (Get-Service), pass all the received services to the sorting cmdlet Sort-Object and indicate that we want to sort them by the Status parameter. At the output, we first get all the services with the status Stop, and then all the services with the status Running.
In the example below, we first get all the services running. After the first pipeline, we go through each element, select only those services for which the status is Running and on the second pipeline we choose what we want to see on the output only the displayname of services:
Get-Service | WHERE {$_.status -eq "Running"} | SELECT displayname
In the example, we use
$ _ . This entry indicates the current item in the pipeline.

Afterword
In this part, we learned how to run PowerShell, and figured out the script execution policy. Understood what are cmdlets, we know how to pass them through the pipeline and how to get their properties. If we forget something, be sure to get-help.
All this knowledge is necessary to make the first leap into the language. Believe me, many more interesting things!
To be continued...
Additional Information