Making Windows PowerShell a great place to work

To get started with PowerShell you will of course need to download and install Windows PowerShell if you don’t have it already. Then I recommend installing a visual editor such as PowerGUI which includes a powerful script editor that includes intellisense for PowerShell.

PowerShell is definitely a very powerful scripting environment, but I couldn’t help but find myself a little frustrated at some small shortcomings in it’s out of the box use. The main problem I have is it isn’t simple to use custom commands or functions from your PowerShell environment from anywhere, at any time. What I really wanted to do was build up a library of functions that I could use in my day to day life with PowerShell that take all the manual labour out of some very simple tasks. I wanted this customisation to be extremely simple too, and here’s what I came up with:

Making PowerShell load your Scripts Library every time

  • Start –> All Programs –> Windows PowerShell * Right Click –> Run As Administrator
  • Type: Set-ExecutionPolicy RemoteSigned
  • Start –> All Programs –> PowerGUI
  • Select File –> Open Current User Profile –> Microsoft.Powershell_profile.ps1
  • Insert the following code:
$profileFolder = Get-Item Env:USERPROFILE
$scriptsFolder = $profileFolder.Value + "\Documents\WindowsPowerShell"
$scriptFiles = Get-ChildItem $scriptsFolder -Include "*.ps1" -Recurse -Exclude "*_profile.ps1"
foreach ($scriptFile in $scriptFiles)
    Write-Host Loading library script $scriptFile
    . $scriptFile

  • Save the file.

Now every time Windows PowerShell starts, it will load all *.ps1 files found in your Documents\WindowsPowerShell folder (except for the *_profile.ps1 you are editing). Now you can build up a library of Script files that declare useful functions for you to work with:

Adding to your Scripts Library

I’ll now give you an example of a handy function I think should be part of my PowerShell environment, it’s called Set-FileCreated (You should always name your functions in the Verb-Noun format). Set-FileCreated simply updates the created, last modified and last accessed times to whatever time you specify (or the current time if not specified)

  • Use your favourite text editor to create a new text file called Set-FileCreated.ps1. The name of the file actually has no bearing on the function you’ll be declaring but it’s a good practice for maintenance purposes to have one function per file, with the file named the same as the function.
  • Add the following text to the file:
Function Set-FileCreated
        [string]$Path=$(Throw "You must specify the name of a file"),
     $file=Get-Item $Path -ea "silentlycontinue"
     if ($file) 
         Write-Host "Touching $path" -foregroundcolor Green
         Write-Warning "Failed to find $path"
  • Now save the file under Documents\WindowsPowerShell\Set-FileCreated.ps1
  • Run PowerShell, Start –> All Programs –> Windows PowerShell (run as administrator not necessary)
  • Now the function will be loaded from the Set-FileCreated.ps1 script and you can run it from anywhere by typing: Set-FileCreated myfile.txt

As I gather useful script functions I’ll be posting them online on my SkyDrive (link below) for anyone to copy into their scripts library folder.

**UPDATE** I’ve changed this from Touch-File to Set-FileCreated to conform to PowerShell’s verb naming convention.

Thanks to Lee Holmes for the Add-OutlookTask script

Have fun!

  1. #1 by Lee on November 15, 2008 - 4:24 am

    Why not just put your functions in scripts in a tools directory? That way, they are easier to share independently, reflect changes immediately (rather than having to dot-source the library again,) and don\’t require this dot-source-everything approach.
    Also, Verb-Noun is great — you should also ensure the verbs are standard:
    Lee Holmes [MSFT]
    Windows PowerShell Development
    Microsoft Corporation

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: