When your PowerShell cmdlet doesn't return anything, use -PassThru

May 11 2013No Comments by justin
The other day I was mounting an ISO in Windows 8 via the Mount-DiskImage command. Since I was mounting the disk image in a script, I needed to know the drive letter it was mounted to so the script could access the files contained within. However, Mount-DiskImage was not returning anything. I didn't want to go through the hack of listing drives before and after I mounted the disk image, or explicitly assigning the drive letter. Both would leave me open to race conditions if another drive was mounted by another process while my script ran. I was at a loss for what to do. Then, I remembered the -PassThru parameter, which I am quite fond of using with Add-Type. See certain cmdlets, like Mount-DiskImage, and Add-Type don't return pipeline output by default. For Add-Type, this makes sense. You rarely want to see a list of the types you just added, unless your exploring the classes in a DLL from the command like. However, for Mount-DiskImage, defaulting to no output was a questionable decision IMHO. Now in the case of Mount-DiskImage, -PassThru doesn't return the drive letter. However, it does return an object that you can pipe to Get-Volume which does return an object with a DriveLetter property. To figure that out, I had to ask on stackoverflow. tl;dr: If your PowerShell cmdlet doesn't return any output, try -PassThru. If you need the drive letter of a disk image mounted with Mount-DiskImage, pipe the output through Get-Volume. For a more in depth treatise of -PassThru, check out this script guy article by Ed Wilson(blog|twitter).

0 Responses to 'When your PowerShell cmdlet doesn't return anything, use -PassThru'

You must be authenticated in order to add a comment.