Monthly Archives: March 2010

Scripting Games 2010

Don’t expect to be anywhere but in front of your computer between 26 April and 7 May.  Why? because the 2010 Scripting Games are happening. The usual 10 events with beginners and advanced versions. A whole bunch of expert commentators … Continue reading

Posted in PowerShell V2 | Leave a comment

Book Review: Windows PowerShell 2.0 Best Practices

Author: Ed Wilson and Windows PowerShell Teams at Microsoft Publisher: Microsoft Press ISBN: 978-0-7356-2646-1 I have three main criteria for judging a book: Is it technically accurate? Does deliver the material it claims to deliver? Is worth the cost of … Continue reading

Posted in PowerShell V2 | Leave a comment

Module Structure

Since PowerShell 2.0 appeared I have been steadily converting a lot of scripts to modules. Up until now I have been using a single .psm1 file (and possibly a .psd1 file for the manifest).  The .psm1 file contains all of … Continue reading

Posted in PowerShell V2 | Leave a comment

Folder names without spaces

Problem How can I prevent the creation of folders with spaces in the name from within my script. Solution 001002003004005006007008009010 function New-Folder {param (    [string]$path = "C:Test",    [string]$name = "")    if($name.Indexof(" ") -ge 0){throw "Folder name contains spaces"}    if (!(Test-Path -Path $path)){throw "Invalid path"}    if ($name -eq ""){throw "Invalid folder name"}    New-Item -Path $path -Name $name -ItemType directory} Define the path and new folder … Continue reading

Posted in Powershell | Leave a comment

Variables I

In my scripts I usually create variables as $a = 3 or similar. There are a number of cmdlets for working with variables Clear-VariableGet-VariableNew-VariableRemove-VariableSet-Variable Lets start with get-variable PS> Get-Variable Name                           Value—-                           —–$                              *variable?                              True^                              Get-Command_args                           {}ConfirmPreference              HighConsoleFileNameDebugPreference                SilentlyContinueError                          {}ErrorActionPreference          … Continue reading

Posted in PowerShell V2 | Leave a comment

Rename files problem

I came across this interesting problem and thought it was worth a post. Problem You have a folder with a set of files with names like this Black or Blue [15665782345].txtBlack or Green [1068682345].txtBlack or White [12345].txtBlack or Yellow [A2G345].txtBlue … Continue reading

Posted in PowerShell V2 | Leave a comment

Paths: IV

The last of our path cmdlets is Convert-Path.  This converts a PowerShell path to a PowerShell provider path. This sequence should explain how it works.   PS> cd HKLM:PS> cd softwarePS> cd microsoftPS> Get-Location Path—-HKLM:softwaremicrosoft PS> Convert-Path -Path (Get-Location)HKEY_LOCAL_MACHINEsoftwaremicrosoft   … Continue reading

Posted in PowerShell V2 | Leave a comment

Paths III

Resolve-Path is interesting in that Resolve-Path c:scripts* "c:scripts*" | Resolve-Path both supply a listing of the contents of the folders.  A sample is shown C:scriptsWMICookBookC:scriptsWPFC:scriptsXMLC:scriptsauto.csvC:scriptscomputers.txtC:scriptsemptyfolders.txtC:scriptsencrypted.txtC:scriptsfirewall.ps1C:scriptsimport-mymodule.ps1   It shows files and subfolders with no way to distinguish. There isn’t a way … Continue reading

Posted in PowerShell V2 | Leave a comment

Paths: II

Two related cmdlets deal with paths – split-path and join-path. Starting with split-path we can use it to split the path to retrieve the file or directory PS> Split-Path -Path C:ScriptsDC02ScriptsDNSnew-mx.ps1C:ScriptsDC02ScriptsDNS PS> Split-Path -Path C:ScriptsDC02ScriptsDNSnew-mx.ps1 -ParentC:ScriptsDC02ScriptsDNS PS> Split-Path -Path C:ScriptsDC02ScriptsDNSnew-mx.ps1 … Continue reading

Posted in PowerShell V2 | Leave a comment

Paths: I

There are a number of cmdlets for working with paths PS> Get-Command *path | select Name Name—-Convert-PathJoin-PathResolve-PathSplit-PathTest-Path   Test-Path is the one I probably use most PS> Test-Path -Path "C:UsersRichardDocumentsPowerShell in PracticeTODO.txt"TruePS> Test-Path -Path "C:UsersRichardDocumentsPowerShell in PracticeChapter1.docx"False It returns a … Continue reading

Posted in PowerShell V2 | Leave a comment