header image

Archive for March, 2010

Scripting Games 2010

Posted by: | March 24, 2010 | No Comment |

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 – including me.   For more information and a few hints on things that could […]

under: PowerShell original

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 purchase and the time I spend reading it? Before diving into the review I have […]

under: Books, PowerShellV2

Module Structure

Posted by: | March 22, 2010 | No Comment |

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 the functions.  Some of my .psm1 files are getting a bit unwieldy so I decided […]

under: Modules, PowerShellV2

Problem How can I prevent the creation of folders with spaces in the name from within my script. Solution 001 002 003 004 005 006 007 008 009 010 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 […]

under: Modules, PowerShell original, PowerShellV2

Variables I

Posted by: | March 18, 2010 | No Comment |

In my scripts I usually create variables as $a = 3 or similar. There are a number of cmdlets for working with variables Clear-Variable Get-Variable New-Variable Remove-Variable Set-Variable Lets start with get-variable PS> Get-Variable Name                           Value —-                           —– $                              *variable ?                              True ^                              Get-Command _ args                           {} ConfirmPreference              High ConsoleFileName DebugPreference                SilentlyContinue Error                          {} ErrorActionPreference          […]

under: PowerShellV2

Rename files problem

Posted by: | March 17, 2010 | No Comment |

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].txt Black or Green [1068682345].txt Black or White [12345].txt Black or Yellow [A2G345].txt Blue or Green [14786862345].txt Pink or Yellow [12345465785].txt Purple or Gold [345612345].txt […]

under: PowerShellV2

Paths: IV

Posted by: | March 15, 2010 | No Comment |

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 software PS> cd microsoft PS> Get-Location Path —- HKLM:\software\microsoft PS> Convert-Path -Path (Get-Location) HKEY_LOCAL_MACHINE\software\microsoft   change drive to the HKLM: registry drive navigate into […]

under: PowerShellV2

Paths III

Posted by: | March 12, 2010 | No Comment |

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:\scripts\WMICookBook C:\scripts\WPF C:\scripts\XML C:\scripts\auto.csv C:\scripts\computers.txt C:\scripts\emptyfolders.txt C:\scripts\encrypted.txt C:\scripts\firewall.ps1 C:\scripts\import-mymodule.ps1   It shows files and subfolders with no way to distinguish. There isn’t a way to recurse through sub folders A System.Management.Automation.PathInfo […]

under: PowerShellV2

Paths: II

Posted by: | March 11, 2010 | No Comment |

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:\Scripts\DC02\Scripts\DNS\new-mx.ps1 C:\Scripts\DC02\Scripts\DNS PS> Split-Path -Path C:\Scripts\DC02\Scripts\DNS\new-mx.ps1 -Parent C:\Scripts\DC02\Scripts\DNS PS> Split-Path -Path C:\Scripts\DC02\Scripts\DNS\new-mx.ps1 -Leaf new-mx.ps1 The default gives the parent container. We can also separate the […]

under: PowerShellV2

Paths: I

Posted by: | March 10, 2010 | No Comment |

There are a number of cmdlets for working with paths PS> Get-Command *path | select Name Name —- Convert-Path Join-Path Resolve-Path Split-Path Test-Path   Test-Path is the one I probably use most PS> Test-Path -Path "C:\Users\Richard\Documents\PowerShell in Practice\TODO.txt" True PS> Test-Path -Path "C:\Users\Richard\Documents\PowerShell in Practice\Chapter1.docx" False It returns a Boolean – true if the path […]

under: PowerShellV2

Older Posts »