Tuesday, January 3, 2017

PowerShell v2 Function CreateFSOIfNotExists

,
I get sick of repeating code. In that spirit this function gives me one line to create a new file (or folder) instead of four (or more) quickly:
function Create-FSOIfNotExists
{
       param(
             [ValidateNotNullOrEmpty()]
             $path,
             [ValidateSet(Directory,File)]
             $itemtype
       )
      
       if(-not(Test-Path -Path $path))
       {
             New-Item -Path $path -ItemType $itemtype |
             Out-Null;
       }
}
All sorts of Verbose output could (probably should) be added to this to make it chattier, but, I just needed a quick utility function and this does the trick.

0 comments to “PowerShell v2 Function CreateFSOIfNotExists”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger