ErrorAction SilentlyContinue


I have the script below that checks if a folder exists, and if it doesn’t it displays the message in Write-host below. The problem is that, it is displaying it, but it also displays the message in bold. I don’t want it to show the message in bold. Would a try / catch work better here?

if ((Test-Path $HomeDirectory) -eq $false)
                    Write-host "ATTENTION: "$Full_Name " home folder path was not found" -ErrorAction SilentlyContinue

NEW-ITEM : The network path was not found
At \hqfs1\users\tantony\PowerShell\HRSecurityForms\HRForm.ps1:112 char:13

  •         NEW-ITEM –path $HomeDirectory -type directory -force | Ou ...
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : WriteError: (\server-8011\users\Person8:String) [New-Item], IOException
    • FullyQualifiedErrorId : CreateIntermediateDirectoriesIOError,Microsoft.PowerShell.Commands.NewItemCommand

I have my script that creates new AD user almost done (thanks to everyone that answered my previous post), just trying to finish it up.

Thank you,


So… you’ve attached -SilentlyContinue to Write-Host, meaning if Write-Host fails, it will not display an error message. You have not in any way prevented Test-Path from throwing an error.

Thank you, how would I do that?

I tried the -ErrorAction in the if statement, and obviously that doesn’t work either.

Well, no, it wouldn’t; If is not a command.

What’s the goal here? Have Test-Path not show an error? Then you add -SilentlyContinue to Test-Path. But your’e not going to know it failed, if that’s the case, so you get no chance to print an alternate message. If you need to CATCH the error and do something about it, you’re going to use -ErrorAction Stop, and a Try/Catch block. See, “The Big Book of PowerShell Error Handling” on our eBooks menu.

Test-path will give you a true or False. I suspect that the error is coming from your variable $Full_Name

$HomeDirectory = "C:\temp1234"

if ((Test-Path $HomeDirectory) -eq $false)
                    Write-host "ATTENTION: $HomeDirectory home folder path was not found"

Also the error in bold is nothing to do with the Code you posted.

NEW-ITEM : The network path was not found
At \hqfs1\users\tantony\PowerShell\HRSecurityForms\HRForm.ps1:112 char:13

  • NEW-ITEM –path $HomeDirectory -type directory -force

The Error is being created by the cmdlet New-Item

The goal is, if the folder doesn’t exist, to display the “Write-host” message only. Not also the one generated by powershell.

I’ll see try/catch block.


Thanks Alex, I’ll check the New-Item error as well

To update…

I have this working now. Some of our servers are named “server-” branch number, and some of them are named after the city. Since I don’t know if they’re named after the city or the branch number, I have a script that looks up the branch number, and if it doesn’t work, it looks up by the city.

Thanks everyone.