PowerShell 5.1 is the current released version of Windows PowerShell and is the version that ships with Windows Server 2016 and Windows Server 2019. It is installed by default on these newer operating systems, but it's also available for install on Windows Server 2008 R2 with Service Pack1, Windows Server 2012, and Windows Server 2012 R2. The last three operating systems must have Windows Management Framework 5.1 installed to support PowerShell 5.1.
PowerShell Commands and Command Structures
When you're working with commands in PowerShell 5.1, you need to understand how to work with PowerShell variables, PowerShell comparison operators, and PowerShell loops and conditionals. You also should know how to use aliases, which are shortcuts for the more common commands.
PowerShell Variables
Creating and working with variables is simple. To create a variable, prefix the variable name with the $ and then give the variable a value:
$loc = Get-Location
To call the variable, you need only type in the $ and the variable name:
$loc
PowerShell Comparison Operators
Comparison operators typically return a true or a false. Here are the common comparison operators in use with PowerShell:
Operator | Description |
---|---|
-gt or -ge | Greater than or greater than or equal to. |
-lt or -le | Less than or less than or equal to. |
-eq or -ne | Equal to or not equal to. |
-and | If both inputs are true, then the output is true. |
-or | If one of the inputs is true, then the output is true. |
-like or -notlike | Uses a wildcard to match patterns. |
-contains and -notcontains | Checks to see if a particular value exists in an array. |
PowerShell Loops and Conditionals
Conditionals are great when you need to evaluate input based on a certain set of criteria and execute a specific block of code based on that evaluation. One of the most common types is the If statement.
$car = 'Nissan' If ($car -eq 'Ford') { Write-Host 'This is a Ford car.' } ElseIf ($car -eq 'Chevrolet') { Write-Host 'This is a Chevrolet car.' } ElseIf ($car -eq 'Nissan') { Write-Host 'This is a Nissan car.' } Else { Write-Host "Don't know what kind of car this is." }
Loops are useful when you want to use the same action against multiple objects. Here are some examples of types of loops.
ForEach
ForEach is used to enumerate a set of data. In the following example, the ForEach is being used to go through all the processes returned from Get-Process and it returns their names.
foreach ($proc in Get-Process){Write-Host $proc.name}
While
Initializes the $num variable with 1 and increments by one with each loop as long as the number is less than or equal to 10. Prints the value of $num with each loop.
$num = 1 while ($num -le 10) { Write-Host $num $num ++ }
Do ... While
Initializes the $myint variable with 1, and then increments the value of $myint by 1 through each loop until $myint is no longer less than or equal to 5. Prints the value of $myint through each loop.
$myint = 1 do { "Starting loop number $myint" $myint $myint++ "Now my integer is $myint" } While ($myint -le 5)
PowerShell Aliases
Aliases are shortcuts for some of the more common commands. You can use an alias much as you would the full command. For example, the following two commands will do the same thing. One is using the alias; the other is not.
Get-Process | Format-Table Get-Process | ft
Here are some of the most frequently used aliases:
Alias | Full Command |
---|---|
gcm | Get-Command |
sort | Sort-Object |
gi | Get-Item |
cp | Copy-Item |
fl | Format-List |
ft | Format-Table |
pwd | Get-Location |
cls | Clear-Host |
ni | New-Item |
sleep | Start-Sleep |
write | Write-Output |
where | Where-Object |
This is an excerpt from: Windows Server 2019 & PowerShell All-in-One For Dummies
Your fast and easy reference to Windows Server 2019 & PowerShell
Windows Server helps to empower businesses around the world to be successful, so every system administrator needs to know a bit about it. These eight minibooks cover the essentials of Windows Server 2019 and PowerShell, including improvements, upgrades, security enhancements, and more. It'll get newbies up to speed and provide a valuable reference for pros.
8 Books Inside...
• Installing & Setting Up Windows Server
• Configuring Windows Server 2019
• Administering Windows Server 2019
• Configuring Networking
• Managing Security
• Working with Windows PowerShell
• Installing and Administering Hyper-V
• Installing, Configuring, and Using Containers
Reader Evan J. Hoover says,"The author does an amazing job of easily laying down the concepts and the requisite knowledge needed to really know what you are doing. I really enjoy the way she describes the subjects and goes into just enough detail to ensure you can confidently proceed with your job, but not so much that it becomes overwhelming (as with other books). This has been a resource that stays open at my desk nearly all the time and I couldn't be more satisfied with the content. Highly recommended." Click here for more information.
More Windows Administration Information:
• Windows Server 2019 and PowerShell All-in-One For Dummies
• Set Up Parental Controls
• Disable Indexing to Speed Up Your Computer
• Search for Text in Text Files
• DOS Switches and Wild Cards
• Easy PC to PC Files and Settings Transfer Cable
• Introduction to DOS
• Disable Windows 10 Automatic Updates
• Windows 10 New Storage View
• Video - Microsoft Remote Desktop - Part One