Skip to content

Instantly share code, notes, and snippets.

@emptyother
Last active November 5, 2017 17:25
Show Gist options
  • Save emptyother/fff66bbb57416561560a3b86b709201d to your computer and use it in GitHub Desktop.
Save emptyother/fff66bbb57416561560a3b86b709201d to your computer and use it in GitHub Desktop.
Powershell template for functions that takes a list of paths or files
#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