Introduction
Windows PowerShell is a scripting program used to automate testing and configuration management. It consists of a command-line shell along with the its associated scripting language.
As with other command lines and terminals, PowerShell syntax is generally comprised of a Verb-Noun
structure and is case-insensitive, and this general syntax may expand as the commands become more complex.
Using PowerShell
CommandPiping We can daisy chain commands to create a Pipeline that manipulates the output of one command using the next command in the pipeline of commands. This is possible because we’re dealing with Objects (C# & .NET) rather than text.
To “Pipe” objects we use the |
symbol between the commands.
For example:
Get-ChildItem | Select-Object Name
Get-ChildItem | Select-Object -First 1
Get-ChildItem | Select-Object -Index 0 | Select-Object Name
In order to make further use of the objects returned by these commands, it’s best to wrap them around with parenthesis. This renders the returned object
to something more similar to what you’d encounter using an OOP language such as C# in which dotNotation can be used to access attributes
(Get-ChildItems | Select -First 1).Name
will return only the name of the first item of the items in the directory.
PowerShell is often used along with Active Directory, which is a Windows datastore management system, particularly to automate the processes that act upon the objects in AD DS frequently.
Operations
PS supports pretty much all the basic operations and control flow conventions of other languages and the syntax is fairly straightforward. However, some of the operators are unconventional and require referencing the documentation, including the basic logic and comparison operators.
Functions
We can define functions in PowerShell using the function
keyword followed by parenthesis that have the parameters and then curly braces to wrap around the function code block.
Like so:
function thisFunc($param1, $param2){
#do something here
}
However, it’s important to remember that when calling a function the syntax is different. It’s like so :
$result = thisFunc "Bob" "Cheeseburger"`
Notice how there are no parenthesis in the function call and how the arguments aren’t separated by commas just whitespace!
Tips
- To get more info on a particular command, type
Get-Help <command>
and PS will spit out more information regarding that command such as syntax, aliases, and remarks. - To further narrow your search, use the asterisk
*
at the start or end (or both) of your help query to signal where in the command it lies. for example:Get-Help *printer*
will return all info on commands that contain “printer” . - To see the commands supported in PowerShell, type
Get-Alias
and that would list a lot of commands and their aliases. - To access the extensive list of ALL the commands type
Get-Command <optional keyword>
. - When typing out a command hitting
Ctrl + space
will show auto-complete options available 3for that command.