Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Invoke an action on a file using a swtich statement of actions
$VerbosePreference = 'Continue'
$action = 'New'
$path = 'c:\somefile.txt'
$result = switch ($action) {
'New' {
Write-Verbose -Message 'Execute New-Item'
New-Item -Path $path
break
}
'Remove' {
Write-Verbose -Message 'Execute Remove-Item'
Remove-Item -Path $path
break
}
'Get' {
Write-Verbose -Message 'Execute Get-Item'
Get-Item -Path $path
break
}
Default {
Write-Verbose -Message 'Invalid Action'
}
}
return $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.