Last active
November 5, 2017 17:25
-
-
Save emptyother/fff66bbb57416561560a3b86b709201d to your computer and use it in GitHub Desktop.
Powershell template for functions that takes a list of paths or files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -version 5 | |
<# | |
.Synopsis | |
Processes a path | |
.Description | |
This is a template for creating Powershell functions that processes paths | |
.Parameter Path | |
The path(s) to process | |
.Inputs | |
System.IO.DirectoryInfo | |
.Inputs | |
String | |
.Outputs | |
System.IO.DirectoryInfo | |
.Example | |
Do-Something -Path . | |
.Example | |
Do-Something -Path ".\bin" | |
#> | |
Function Do-Something { | |
[Cmdletbinding()] | |
Param( | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[ValidateScript({ | |
$_ | ForEach-Object { | |
(Get-Item $_).PSIsContainer | |
} | |
})] | |
[System.IO.DirectoryInfo[]] | |
$Path | |
) | |
Begin { Write-Verbose "Starting script..." } | |
Process { | |
Try { | |
foreach($p in $Path) { | |
if(Test-Path -Path $p) { | |
$item = Get-Item $p; | |
Write-Verbose ("Processing item `"{0}`"" -f $item.FullName); | |
# Do stuff | |
Write-Output $item; | |
} | |
} | |
} | |
Catch { | |
} | |
} | |
End { Write-Verbose "Ending script..." } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment