Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Invoke an action on a file using a hash table of actions and null coalescing to report invalid action
$VerbosePreference = 'Continue'
$action = 'New'
$path = 'c:\somefile.txt'
$actions = @{
'New' = {
Write-Verbose -Message 'Execute New-Item'
New-Item -Path $path
'Remove' = {
Write-Verbose -Message 'Execute Remove-Item'
Remove-Item -Path $path
'Get' = {
Write-Verbose -Message 'Execute Get-Item'
Get-Item -Path $path
return ($actions[$action], {Write-Verbose -Message 'Invalid Action'}, 1 -ne $null)[0].Invoke()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment