PowerShell for beginners

When working with PowerShell, the first thing we encounter is commands (Cmdlets).
The command call looks like this:

Verb-Noun -Parameter1 ValueType1 -Parameter2 ValueType2[]

Help

Help in PowerShell is accessed using the Get-Help command. One of the parameters can be specified: example, detailed, full, online, showWindow.

Get-Help Get-Service -full will return the full description of the operation of the Get-Service command
Get-Help Get-S* will show all available commands and functions starting with Get-S*

There is also detailed documentation on the official Microsoft website.

Here is an example help for the Get-Evenlog command

PowerShell for beginners

If parameters are enclosed in square brackets [], then they are optional.
That is, in this example, the name of the log itself is required, and the name of the parameter No. If the parameter type and its name are enclosed in brackets together, then this parameter is optional.

If you look at the EntryType parameter, you can see the values ​​\uXNUMXb\uXNUMXbthat are enclosed in curly brackets. For this parameter, we can only use predefined values ​​in curly braces.

Information about whether the parameter is required can be seen in the description below in the Required field. In the example above, the After attribute is optional because Required is set to false. Next, we see the Position field opposite which says Named. This means that you can refer to the parameter only by name, that is:

Get-EventLog -LogName Application -After 2020.04.26

Since the LogName parameter had the number 0 instead of Named, this means that we can refer to the parameter without a name, but by specifying it in the desired sequence:

Get-EventLog Application -After 2020.04.26

Let's assume this order:

Get-EventLog -Newest 5 Application

Alias

So that we can use the usual commands from the console in PowerShell, there are aliases (Alias).

An example alias for the Set-Location command is cd.

That is, instead of calling the command

Set-Location “D:”

we can use

cd “D:”

History

To see the history of command calls, you can use Get-History

Execute command from history Invoke-History 1; Invoke History 2

Clear-History

Pipeline

A pipeline in powershell is when the result of the first function is passed to the second. Here is an example using the pipeline:

Get-Verb | Measure-Object

But to better understand the pipeline, let's take a simpler example. Got a team

Get-Verb "get"

If you call the Get-Help Get-Verb -Full help, then we will see that the Verb parameter takes pipline input and ByValue is written in brackets.

PowerShell for beginners

This means that we can rewrite Get-Verb "get" to "get" | GetVerb.
That is, the result of the first expression is a string and it is passed to the Verb parameter of the Get-Verb command via pipline input by value.
Also pipline input can be ByPropertyName. In this case, we will pass an object that has a property with a similar name Verb.

Variables

Variables are not strongly typed and are specified with a $ in front

$example = 4

The symbol > means to put the data in
For example, $example > File.txt
With this expression, we will put the data from the $example variable into a file
Same as Set-Content -Value $example -Path File.txt

Arrays

Array initialization:

$ArrayExample = @(“First”, “Second”)

Empty array initialization:

$ArrayExample = @()

Getting value by index:

$ArrayExample[0]

Get the whole array:

$ArrayExample

Adding an element:

$ArrayExample += “Third”

$ArrayExample += @(“Fourth”, “Fifth”)

Sorting:

$ArrayExample | Sort

$ArrayExample | Sort -Descending

But the array itself remains unchanged with this sorting. And if we want the array to have sorted data, then we need to assign the sorted values:

$ArrayExample = $ArrayExample | Sort

There is no way to remove an element from an array in PowerShell, but you can do it like this:

$ArrayExample = $ArrayExample | where { $_ -ne “First” }

$ArrayExample = $ArrayExample | where { $_ -ne $ArrayExample[0] }

Removing an array:

$ArrayExample = $null

Loops

Loop syntax:

for($i = 0; $i -lt 5; $i++){}

$i = 0
while($i -lt 5){}

$i = 0
do{} while($i -lt 5)

$i = 0
do{} until($i -lt 5)

ForEach($item in $items){}

Exit from the break loop.

Skip the continue element.

Conditional Statements

if () {} elseif () {} else

switch($someIntValue){
  1 { “Option 1” }
  2 { “Option 2” }
  default { “Not set” }
}

Function

Function definition:

function Example () {
  echo &args
}

Function launch:

Example “First argument” “Second argument”

Defining arguments in a function:

function Example () {
  param($first, $second)
}

function Example ($first, $second) {}

Function launch:

Example -first “First argument” -second “Second argument”

Exception

try{
} catch [System.Net.WebException],[System.IO.IOException]{
} catch {
} finally{
}

Source: habr.com

Add a comment