'Hide output of -WhatIf parameter when using New-Item

I want to test if New-Item throws an error, for which i am using the -WhatIf parameter and catch the error if an error occurs. If no error occurs, i dont want the -WhatIf output in my console.

I tried the following, but the "WhatIf:"-Output still gets printed to the Powershell console:

New-Item -Path $Path -Name $Name -ItemType $ItemType -ErrorAction stop -WhatIf | Out-Null

I also tried to use $WhatIfPreference as in this StackOverflow answer, which had the same result.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source