powershell
Intro
PowerShell is a powerful tool for both IT professionals and developers because it offers:
- Rich scripting and automation: It lets you automate repetitive tasks across many servers, saving time and ensuring consistency.
- Interactive shell environment: You can run commands interactively to manage and configure systems efficiently.
- Object-oriented + Developer-native approach: Everything you work with in PowerShell is treated as an object, making it intuitive and powerful, especially since it's based on the .NET framework.
Command syntax

- cmdlet: A cmdlet is a combination of a verb and a noun/resource, like
Get-ServiceorGet-Help. - command: A powershell command is a combination of a cmdlet and parameters to pass to the cmdlet.
IMPORTANT
Powershell is case-insensitive
When you run a powershell command, it returns an object

IMPORTANT
When passing in parameters, you can use wildcard syntax with *.

Parameter types
There are two types of parameters you can pass to a cmdlet in order to make it a command:
- positional parameters: parameters specified without flags
- named parameters: parameters specified with flags or they are another way to explicitly name positional parameters
IMPORTANT
Positional parameters always have named parameter variants, but not the other way around.
Some named parameters cannot be positional parameters.
Parameter sets
In PowerShell, a parameter set is a way for a single cmdlet to support different groups of parameters for various related tasks.
Each parameter set defines a unique combination of parameters that can be used together, ensuring only valid parameter combinations are accepted.
- This design helps reduce errors and makes cmdlets more flexible and user-friendly.
- For example, the Get-EventLog cmdlet uses different parameter sets to query events by log name or event ID, allowing you to perform different queries with the same command but different parameters.
Backticks
Backticks are used for line continuation in long commands.
# Backticks (`) for Line Continuation
Get-Process -Name `
"notepad", `
"powershell"
Splatting
In PowerShell, splatting is a technique that lets you group multiple parameters into a single variable (usually a hash table or array) and then pass them all at once to a cmdlet or function using the @ symbol.
This makes your scripts cleaner and easier to read, especially when dealing with commands that require many parameters. Instead of writing a long command line with lots of parameters, you define them upfront in a variable and then pass that variable to the command.
Instead of:
Get-Process -Name "notepad", "powershell"
We can just pass a hastable of those same named parameter values to the command:
# Splatting
$params = @{
Name = "notepad"
}
Get-Process @params
$processParams = @{
Name = "notepad"
ComputerName = "Server01"
ErrorAction = "SilentlyContinue"
}
Get-Process @processParams
Important cmdlets
Getting help
The Get-Help cmdlet is a universal cmdlet that takes in another cmdlet as an argument and returns help information about that cmdlet:
Get-Help <cmdlet>
You also have these additional options to organize how the help information comes back:
-examples: returns command examples-detailed: returns detailed text information.-full: returns all info.-online: links you to the online documentation for the command.
Get-Help Format-Table | Out-Host -Paging
# Display more information for a cmdlet
Get-Help Format-Table -Detailed
Get-Help Format-Table -Full
# Display selected parts of a cmdlet by using parameters
Get-Help Format-Table -Examples
Get-Help Format-Table -Online
Get-Help Format-Table -Parameter *
Get-Help Get-ChildItem -Parameter *
Get-Help Format-Table -Parameter GroupBy
Viewing help info on parameters of a cmdlet
For named parameters for a cmdlet you can view help info for them by following this syntax and specifying the parameter with the -Parameter <ParameterName> flag:
Get-Help Get-Process -Parameter Name
Listing commands with Get-Command
The Get-Command lists all possible commands in powershell.
Get-Command
# returns list of all commands that start with "Write"
Get-Command Write-*
# returns list of all commands that start with "Get"
Get-Command Get-*

You have these named parameters:
-Verb <verb>: the verb to filter on, likeGet,Remove, etc.-Noun <noun>: the noun to filter on, likeProcess,Service, etc.-CommandType <type>: the command type to filter on, likeAliasto filter down to only alias commands. Here are the different types:CmdletFunctionAlias
-Name <name>: the name of a certain cmdlet to find.- This is also the 1st positional parameter.
-Syntax: outputs the syntax of how to use the cmdlet or function.
The Get-Command cmdlet also works with functions:
Get-Command -Name <function-name> -Syntax
Protecting against destructive commands
You have two additional flags you can add to any cmdlet that starts with Write or Remove which will help you protect against running destructive actions blindly.
-whatif: performs a dry run of a command without actually executing it, just showing what would be outputted.-confirm: asks before performing the destructive action on each item in the list of objects returned by the command
-whatif
The -whatif flag lets you perform a dry-run of a command without actually running it, just to see what the output would be.
Normally the command below, where you're piping a list of service objects into the Stop-Service cmdlet, would kill every single process on your computer and force you to restart.
Get-Service | Stop-Service
But with the -whatif flag it lets you perform a dry run and view the output of the command without actually running it:
Get-Service | Stop-Service -whatif
-confirm
The -confirm flag will ask you to confirm the command execution for each object the operation is being performed on.
Get-Service | Stop-Service -confirm
Aliases
Powershell bridges the gap for bash developers by providing aliases for common bash commands and mapping them to the underlying powershell command.
For example, the alias for the bash ls command maps to the Get-ChildItem command in powershell, which is what actually lists a directory.

WARNING
Use aliases sparingly, and don't use them in scripts. This is because you need powershell scripts to be as easy to understand as possible.
Here's how to create an alias:
New-Alias <AliasName> <Cmdlet>
New-Alias getp Get-Process
Heres how to retrieve an alias
Get-Alias pwd
Get-Alias -Definition pwd
Variables and values
In Powershell, variables are prefixed with a $, and you refer to them in this syntax:
$variableName
You can set variables like so:
$variableName = value
Primitive variables basics
There are four different types of primitive data types you can store:
- string: string value represented by text within double quotes
# stores the value of "Aadil" in the `$UserName` variable.
$UserName = "Aadil"
- number: stores a numeric value, either integer or float.
$Age = 22
Write-Host "Hello, you are $Age years old"
- boolean: stores a conditional value, either
$Truefor true or$Falsefor false.
$IsActive = $True
If ($IsActive) {
Write-Host "is active is true"
}
Else {
Write-Host "is active is false"
}
- null: null value represented by
$Nullvalue.
Variables are under the hood an object instance of a certain class, and the same goes for primitive data type variables.
For example:
- strings: of type
String - numbers: of type
IntorInt32if integer, orDoubleif floating point. - boolean: of type
Boolean
Object variable basics
Besides that, you also have object, hashmap, and array types:
- object or object list: you can store the results of commands as variables, just like you can in bash
$Processes = Get-Process
$Processes | Format-List
Strings
With strings you can concatenate them using the + operator, which is useful for creating new strings by joining other strings and string variables:
$Name = "Aadil"
$Age = 22
$Greeting = "Hello, my name is " + $Name + " and I am $Age years old"
Escaping strings
To escape certain characters in strings, you don't use a backslash. Instead, you use a backtick:
Write-Host "`n`n loser"
String object
For all following notation, we will store an object instance of the String class (a normal string) in a $String variable.
Here are the properties on a string object:
$String.Length: returns the length of the string
Here are the methods on a string object:
$String.Substring(start: Int, end: Int): returns a substring$String.Replace(old: String, new: String): replace the first matching string with the new string.$String.ToLower(): return the string as lowercase$String.ToUpper(): return the string as uppercase