Getting started with PowerShell
Windows PowerShell is a shell and scripting component of the Windows Management Framework, an automation/configuration management framework from Microsoft built on the .NET Framework. PowerShell is installed by default on all supported versions of Windows client and server operating systems since Windows 7 / Windows Server 2008 R2. Powershell can be updated at any time by downloading a later version of the Windows Management Framework (WMF). The "Alpha" version of PowerShell 6 is cross-platform (Windows, Linux, and OS X) and needs to be downloaded and installed from this release page.
- MSDN Documentation: https://msdn.microsoft.com/en-us/powershell/scripting/powershell-scripting
- TechNet: https://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx
- PowerShell Gallery: https://www.powershellgallery.com/
- MSDN Blog: https://blogs.msdn.microsoft.com/powershell/
- Github: https://github.com/powershell
- Community Site: http://powershell.com/cs/
|Version||Included with Windows||Notes||Release Date|
|1.0||XP / Server 2008||2006-11-01|
|2.0||7 / Server 2008 R2||2009-11-01|
|3.0||8 / Server 2012||2012-08-01|
|4.0||8.1 / Server 2012 R2||2013-11-01|
|5.0||10 / Server 2016 Tech Preview||2015-12-16|
|5.1||10 Anniversary edition / Server 2016||2017-01-27|
Aliases & Similar Functions
In PowerShell, there are many ways to achieve the same result. This can be illustrated nicely with the simple & familiar
Hello World example:
It's worth noting that although
Write-Host both write to the screen there is a subtle difference.
Write-Host writes only to stdout (i.e. the console screen), whereas
Write-Output writes to both stdout AND to the output [success] stream allowing for redirection. Redirection (and streams in general) allow for the output of one command to be directed as input to another including assignment to a variable.
These similar functions are not aliases, but can produce the same results if one wants to avoid "polluting" the success stream.
Write-Output is aliased to
Or, by simply typing 'Hello world'!
All of which will result with the expected console output
Another example of aliases in PowerShell is the common mapping of both older command prompt commands and BASH commands to PowerShell cmdlets. All of the following produce a directory listing of the current directory.
Finally, you can create your own alias with the Set-Alias cmdlet! As an example let's alisas
Test-NetConnection, which is essentially the PowerShell equivalent to the command prompt's ping command, to "ping".
Now you can use
ping instead of
Test-NetConnection! Be aware that if the alias is already in use, you'll overwrite the association.
The Alias will be alive, till the session is active. Once you close the session and try to run the alias which you have created in your last session, it will not work. To overcome this issue, you can import all your aliases from an excel into your session once, before starting your work.
Allow scripts stored on your machine to run un-signed
For security reasons, PowerShell is set up by default to only allow signed scripts to execute. Executing the following command will allow you to run unsigned scripts (you must run PowerShell as Administrator to do this).
Another way to run PowerShell scripts is to use
Or from within your existing PowerShell console or ISE session by running:
A temporary workaround for execution policy can also be achieved by running the Powershell executable and passing any valid policy as
-ExecutionPolicy parameter. The policy is in effect only during process' lifetime, so no administrative access to the registry is needed.
There are multiple other policies available, and sites online often encourage you to use
Set-ExecutionPolicy Unrestricted. This policy stays in place until changed, and lowers the system security stance. This is not advisable. Use of
RemoteSigned is recommended because it allows locally stored and written code, and requires remotely acquired code be signed with a certificate from a trusted root.
Also, beware that the Execution Policy may be enforced by Group Policy, so that even if the policy is changed to
Unrestricted system-wide, Group Policy may revert that setting at its next enforcement interval (typically 15 minutes). You can see the execution policy set at the various scopes using
Calling .Net Library Methods
Static .Net library methods can be called from PowerShell by encapsulating the full class name in third bracket and then calling the method using
Static methods can be called from the class itself, but calling non-static methods requires an instance of the .Net class (an object).
For example, the AddHours method cannot be called from the System.DateTime class itself. It requires an instance of the class :
In this case, we first create an object, for example :
Then, we can use methods of that object, even methods which cannot be called directly from the System.DateTime class, like the AddHours method :
To comment on power scripts by prepending the line using the
# (hash) symbol
You can also have multi-line comments using
#> at the beginning and end of the comment respectively.
New-Object cmdlet is used to create an object.
In many instances, a new object will be created in order to export data or pass it to another commandlet. This can be done like so:
There are many ways of creating an object. The following method is probably the shortest and fastest way to create a
In case you already have an object, but you only need one or two extra properties, you can simply add that property by using
All objects can be stored in variables or passed into the pipeline. You could also add these objects to a collection and then show the results at the end.
Collections of objects work well with Export-CSV (and Import-CSV). Each line of the CSV is an object, each column a property.
Format commands convert objects into text stream for display. Avoid using Format-* commands until the final step of any data processing, to maintain the usability of the objects.
Installation or Setup
PowerShell is included with the Windows Management Framework. Installation and Setup are not required on modern versions of Windows.
Updates to PowerShell can be accomplished by installing a newer version of the Windows Management Framework.
"Beta" version of PowerShell 6 can be installed on other platforms. The installation packages are available here.
For example, PowerShell 6, for Ubuntu 16.04, is published to package repositories for easy installation (and updates).
To install run the following:
After registering the Microsoft repository once as superuser,
from then on, you just need to use
sudo apt-get upgrade powershell to update it.
Then just run
The Pipeline - Using Output from a PowerShell cmdlet
One of the first questions people have when they begin to use PowerShell for scripting is how to manipulate the output from a cmdlet to perform another action.
The pipeline symbol
| is used at the end of a cmdlet to take the data it exports and feed it to the next cmdlet. A simple example is using Select-Object to only show the Name property of a file shown from Get-ChildItem:
More advanced usage of the pipeline allows us to pipe the output of a cmdlet into a foreach loop:
Note that the example above uses the $_ automatic variable. $_ is the short alias of $PSItem which is an automatic variable which contains the current item in the pipeline.