As a System Administrator I’m always looking for automation and easier or faster ways to do my job properly. When I’m creating scripts which involve of creating or checking if something exists it prevents errors in output and is a nicer and cleaner way to go about doing scripts, why would you want to create something exists anyways? Writing scripts or snippets I use the command Test-Path combined with an if statement and it depends what I want to do if I’m going to use the negative “NOT”, let me give you an example.
$path = "C:\temp"
if(Test-Path -Path $path) {
 Write-Host "The path `"$path`" already exists"
} else {
 New-Item -Path $path -ItemType Directory
}
Here is another sample which I turn it around.
$path = "C:\temp"
if(!(Test-Path -Path $path)) {
 New-Item -Path $path -ItemType Directory
} else {
 Write-Host "The path `"$path`" already exists"
}
Both examples provide the same outcome but it depends on what you are trying to achieve. I hope you enjoyed this small knowledge for now. Hallgrimur #TheMachine