The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

pianoboy (6) [Avatar] Offline
First I would like to commend all the authors for the clear and concise way the book is written. With many chapters (40) but with each chapter averaging 15 pages it is easy to use the book as a reference source.

However, the overall tone of the Chapter 27 summary is that it is something for the propeller heads and that an administrator would not really need to use them. To some extent this is true as an extension does not do anything that can not be done by a script.

As you point out, extensions make a script easy to read. But a major benefit that is overlooked is that it adds intellisense to an object. This can improve object re-use in an organization.
As an example, a module may be made available to all administrators that includes a cmdlet
Get-FileName that gets the file-name portion from a full file name path string. In large modules remembering this cmdlet/script name may not be so easy so most developers would just roll their own script to do this. The more often they do this in scripts,the greater likelihood that errors are introduced.

If instead, an extension method GetFileName () was created for the system.string type, whenever an administrator has a string file path, simply by adding the dot operator the method would become A visible reminder to use the standard method. This is especially so if using an editor such as the PS3 Powershell_ISE.

I have included a small script below that will perhaps provide your readers with a better appreciation of the power of extension methods and how they can better be used to improve readability and encourage the use of standard methods.

As seen in the following line,"C:WindowsSystem32WindowsPowerShellv1.0Help.Format.ps1xml".IsFileType(".ps1xml") is more easily understood than would be a call to an equivalent function as it reads more like file path is file type .ps1xml ?".


Update-TypeData -TypeName System.String -MemberType ScriptMethod -MemberName IsValidPath -Value {
Test-Path -Path $this
} -ErrorAction SilentlyContinue

Update-TypeData -TypeName System.String -MemberType ScriptMethod -MemberName ToFile -Value {
if (Test-Path $this -PathType container )
Write-Error -ErrorAction Stop `
-Exception System.InvalidOperationException `
-Message "Path is a directory! Supply a full path name to file."
Get-Item $this } -ErrorAction SilentlyContinue

Update-TypeData -TypeName System.String -MemberType ScriptMethod -MemberName GetParentDirectory -Value {
(Get-Item $this).Parent.FullName} -ErrorAction SilentlyContinue

Update-TypeData -TypeName System.String -MemberType ScriptMethod -MemberName GetFileName -Value {
if (Test-Path $this -PathType container )
Write-Error -ErrorAction Stop `
-Exception System.InvalidOperationException `
-Message "Path is a directory! Supply a full path name to file."
(Get-Item $this ).Name} -ErrorAction SilentlyContinue

Update-TypeData -TypeName System.String -MemberType ScriptMethod -MemberName GetFileExtension -Value {
(Get-Item $this ).Extension} -ErrorAction SilentlyContinue

Update-TypeData -TypeName System.String -MemberType ScriptMethod -MemberName IsFileType -Value {
(Get-Item $this ).Extension -eq $args[0]} -ErrorAction SilentlyContinue

"C:/Windows/System32".IsValidPath() # smart enough to recognize as valid
"..Chapter22".IsValidPath() # depends if you have folder in relative path
"/etc/fstab".IsValidPath() # unix path false

"C:WindowsSystem32WindowsPowerShellv1.0".ToFile().ToString() # Will throw exception as not file
"C:WindowsSystem32WindowsPowerShellv1.0 ubbishfilename.txt".ToFile().ToString() # Will throw exception as doesnot exist


"C:WindowsSystem32WindowsPowerShellv1.0".GetFileName() #Will throw exception as not file