How to Negate powershell conditions
While developing some new functions for one of my Powershell modules today, I faced the problem where I had to negate one of my functions.
To be more specific, I have a function that returns $True if the Powershell session/user has administrator privileges. I then had to display an error cause the function had to be able to access system files.
The solution is very simple. Lets say we have the following statement:
To be more specific, I have a function that returns $True if the Powershell session/user has administrator privileges. I then had to display an error cause the function had to be able to access system files.
The solution is very simple. Lets say we have the following statement:
if (Test-Path C:\Scripts) {
Write-Host "Path exists."
}
and we want to negate it. The only thing we have to do is enclose the Test-Path check into Not function:
if (-Not (Test-Path C:\Scripts)) { Write-Error "Path does not exist!" }