Powershell notes
1. What is Powershell & cmdlets?
Powershell (PS) is the Windows Scripting Language built using the .NET framework. PS is able to execute .NET functions directly from its shell. PS commands are called cmdlets - most of them is written in .NET. The output of cmdlets are objects. This approach makes PS shell modular - it's easy to apply some actions on the output objects or pass them to another cmdlet.
Format of cmdlet command: Verb-Noun. Common verbs:
- Get
- Start
- Stop
- Read
- Write
- New
- Out
- Invoke
1.1. PowerShell scripts
Powershell ISE is the Powershell Text Editor most often used to write longer PowerShell scripts. Most common extension of PowerShell files is .ps1
.
1.2. What is a cmdlet?
Cmdlets (pronounced: command-lets) are native PS commands, not stand-alone executables. Cmdlets are collected into PowerShell modules that can be loaded on demand. They can be written in any compiled .NET language or in the PS scripting language itself. Cmdlets return .NET objects.
NOTE: Cmdlets and their parameters are case-insensitive. However, Microsoft generally recommends entering a PowerShell cmdlet (or a parameter) with the first letter of each word capitalized.
2. Pipeline
To pass output from one cmdlet to another the pipline is used. Instead of passing text, PowerShell passes a .NET object to next cmdlet. Object contains methods and properties. Objects returned by the last command in a chain are printed out on the screen.
3. Variables
4. If statement
NOTE: String comparisions are case-insensitive unless you use the explicit case-sensitive operator. To make a comparison operator case-sensitive, add a
c
after the-
(-ceq
is the case-sensitive version of-eq
).
Most common: