Introduction
This page contains links to a wide variety of sample PowerShell scripts,
indexed and organised by the nature of the
script. These scripts were developed both as a learning exercise, and to
increase the productivity and simplify the use PowerShell and include function,
filters, cmdlets, snap-ins, build scripts etc.
NB: The scripts on this page have not all been validated and
updated for PowerShell RC1.1. The scripts shown with a ps1 extension have
been updated and tested. The rest will get updated as soon as possible
There are two ways to use these scripts:
- Use the sample scripts as a learning tool - Download
a script cut/paste them into a PS console. Or use your favourite text editor
and work with them. Explore the PowerShell features used in each script to help you
learn more.
- Leverage the scripts into your environment - Download
one or more of the samples and copy the function definitions to your Profile.PS1
file.
Also, add any new aliases that make sense in your environment. That way, you have them all in your environment as
you use PowerShell interactively.
A zip of all the scripts on this page is at
http://www.reskit.net/monad/samplescripts/samplescripts.zip. NB: on
occasion, this zip file is a little out of date - it takes a while after
posting a new script for the zip to get updated. See the
change log for changes made to this page and the scripts on the page.
- Get-WhoAmI.ps1 - defines
a function (Get-WhoAmI) and an alias (WhoAmI) to display your domain and
username (and an indication if you're administrator!). Tested on PowerShell
RC1.1.
- Display-OSInfo.ps1 - displays
information about the OS, based on WMI. Tested on PowerShell RC1.1.
- Ngen-PowerShell.ps1
- PowerShell R1.1 does not always properly generate native code for PowerShell, which means a
perf hit at start5up as the MSIL in PowerShell is compiled into native code. This
script calls Ngen.exe to generate native code, which speeds up Msh.exe load
time.
Since PowerShell is based on .NET, you can use the
System.DirectoryServices .NET class to access Active Directory.
- Display-ADUsers1.ps1 - this script prints out the user in the Users
Container in the Kapoho.Net domain. You can modify it to suit your domain
and OUs.
- Remove-Alias.ps1
- removes an
alias from the lists of aliases. The remove-alias cmdlet was dropped from
PowerShell V1, but you can get it here for free. Just add this to your Profile.ps1.
- Add-ProgrammerAliases.ps1
- do you hate the names of the Cmdlets? Well now you have options! If you
are a programmer, you might just hate the "-" you have to use in cmdlets
and want to do away with it? This script defines a function
that adds aliases for all commands, sans "-". Also adds apa alias for
this function.
- Add-ProgrammerAliases2.ps1
- this variant of Add-ProgrammerAliases adds aliases that have a "->" between
verb and noun. Also adds apa2 alias for this function.
- Add-ProgrammerAliases3.ps1
- This variant adds aliases with underlines between verb and noun. Also
adds apa3 alias for this function.
These three scripts work together to help you to get ready to
digitally sign PowerShell scripts. First, you run Create-LocalCertRootCert to
creeate and selfsign a trusted root certificate. Next run Create-SigningCert.ps1
to create and install a code-signing certificate. You can obviously remove the
root and signing certificates using MMC's Certificate snap-in and start again!
-
Create-LocalCertRootCert.ps1
- This script creates a local root code signing cert and installs it into your
trusted root store.
-
Create-SigningCert.ps1
- This script, which you run after running Create-LocalCertRootCert.p1, creates
a signing cert and sticks it into your private cert store for use.
-
Sign-File.ps1 - this
script (run after the above two) uses the code signing cert created by Create-SigningCert.ps1,
to sign a file. If you have multiple code signing certs, this script uses
the first one found.
- Set-PowerShellPrompt.ps1 - sets the prompt in
Powershell to be "[<cwd>]:".
Just import this function definition into your Profile.ps1 to change your profile to
your liking!
- Set-MSHPrompt2.ps1 -
another prompt setter, which shows the PowerShell history id as part of prompt.
- Write-HostBold.ps1 - writes
text to the PowerShell console with a different foreground/background colour to the normal
console to make the text stand out. Adjust this for what is
"bold", based on your console colours.
- Write-HostBold2.ps1 -
another version of Write-HostBold, this time writing directly to the host.
- CopyfromConsole.ps1 - this script defines a function (CopyfromConsole) and
an alias (copycon). Copycon copies a file from the console to a named
file. PowerShell does not implement the con: pseudo-file, so this function provides a
useful alternative.
- List-FolderContentsInColour.ps1
- lists the contents of a folder using different colours to indicate different
types of files/folders. Customise to your own taste and usage. This is the first
script to have been submitted by anyone for inclusion here - thanks to Ron
Williams.
- Get-DiskSize.ps1 - Shows
the size of each disk on a system. This script takes a computer name as a
parameter (with a default of Localhost). This sample also demonstrates formatting
of numbers within a string, as well as showing how PowerShell converts numbers
(i.e. of bytes) into numbers of gigabytes.
- Display-DiskFreeSpace.ps1- this script defines a function, Get-DiskFreeSpace
that prints out the free space percentages for all drives on a system. This
script accesses raw Win32 performance data using WMI. The script also
defines an alias, DDFS.
- Get-HeadOfFile.ps1 -
defines a function and an alias (Head) that gets the first N lines
of a file (where 10 is the default).
- Get-TailOfFile.ps1 -
defines a function and an alias (Tail) that gets last N lines of a
file (with 10 as the default).
- Touch-File.ps1 - defines a
function and an alias (Touch) that creates a file if it does not
already exist. It also creates a first line in the new file (#<filename>) as an aid.
- Format-Number.ps1 - this script shows some ways to format numbers and
strings. It demonstrates some ways you can use .NET Format strings and the
ToString() function to give you more control over formatting and layout. Run
the script, then leverage the mechanisms demonstrated. Uses Write-HostBold
function.
Hash Tables
Hash tables are a special type of keyed file array, and a very
useful Monad language feature.
- HashTable.ps1 - a
script to setup a deep hash table, and to show how to address elements. Uses
Write-HostBold function.
-
Display-HelpSyntax.ps1 - takes a cmdlet name as a parameter and displays
the cmdlet's syntax, if it exists. This function also mimics the behaviour
of Get-Help's handling multiple matches.
This script also
defines two aliases (ghs and dhs).
- Return-Stuff.ps1 - this script
shows how you can return information from a function (or from a script) to a
calling script. Return-Stuff.p1
defines a function and then calls the function demonstrating how variables are
returned from that function call.
- Run-YubnubCommand.ps1 -
this function runs a command from the popular Yubnub site. An alias is also
created (ryyc).
- Search-MyDelicious.ps1
- this script defines a function to search "my" delicious (see
http://del.icio.us for details of
this great site). You need to define your user name - the script uses the
author's id (which you can of course use to find stuff! The script also
defines an alias, smd. Smd, like ryyc, brings up the results of the command
in a browser window, using your default browser.