Change Get-PWFolders to throw error on missing folder rather than warning

Can you change Get-PWFolders to throw a catchable error rather than just issue a warning?

$pwf = Get-PWFolders -FolderPath "Folder 1\Folder 2"
if ($pwf -eq $null) {
    folder doesn't exist?
    do stuff?
}

try {
    $pwf = Get-PWFolders -FolderPath "Folder 1\Folder 2"
} catch {
    Error definitely occurred
}

I prefer the 2nd coding example as it allows me to catch different error types and seems the more powershell appropriate way

Parents
  • We can look into this change.   What I've been doing is adding a Throw to my an if / else statement or catching the warning in a try/catch.

    $FolderPath = "Folder 1\Folder 2"
    $pwf = Get-PWFolders -FolderPath $FolderPath
    if ([string]::IsNullOrEmpty($pwf)) {
        Throw "Failed to locate folder '$FolderPath'."
    }
    
    try {
        $pwf = Get-PWFolders -FolderPath $FolderPath -WarningAction Stop -WarningVariable wv
    } catch {
        Throw "Failed to locate folder '$FolderPath'. Message: $($wv[0])"
    }

Reply
  • We can look into this change.   What I've been doing is adding a Throw to my an if / else statement or catching the warning in a try/catch.

    $FolderPath = "Folder 1\Folder 2"
    $pwf = Get-PWFolders -FolderPath $FolderPath
    if ([string]::IsNullOrEmpty($pwf)) {
        Throw "Failed to locate folder '$FolderPath'."
    }
    
    try {
        $pwf = Get-PWFolders -FolderPath $FolderPath -WarningAction Stop -WarningVariable wv
    } catch {
        Throw "Failed to locate folder '$FolderPath'. Message: $($wv[0])"
    }

Children