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 |
How to run PowerShell scripts
To create and run a PowerShell script, follow these steps:
- Create your PowerShell script and save it as a PS1 file.
This is the PowerShell file extension. - Open a PowerShell window by right-clicking Start and then choosing Windows PowerShell (Admin).
- Navigate to the directory the script is located in and then type the following to execute the script from the current directory:
.\yourscript.ps1
If you get an error when you try to run your script, it may be due to your execution policy being set too restrictive. Try running the following:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
How to access PowerShell help
It’s always useful to know how to get help in PowerShell if you get stuck on the proper usage of a command. Here are helpful ways to get some assistance on the command line.
To update the help files on your system, run this cmdlet:
Update-Help
Running the following command will display the basic help file for the Get-Process
command, including a description and the appropriate syntax. You can run Get-Help
for any cmdlet, not just Get-Process
.
Get-Help Get-Process
Running the command with the -Detailed
parameter will give you additional information like descriptions of the parameters and examples of how to use the cmdlet.
Get-Help Get-Process -Detailed
Running the command with the -Full
parameter will give you additional information like descriptions of the parameters and examples of how to use the cmdlet, as well as the types of input/output objects and any additional notes that might appear in the help file.
Get-Help Get-Process -Full
The last cmdlet you should know is the Get-Member
cmdlet. If you aren’t sure how to interact with a cmdlet, Get-Member
can give you the methods and the properties associated with the cmdlet.
Get-Process | Get-Member