How to check if file or folder exists in Powershell

To check if file or folder exists in Powershell you can use the following snippet.

Sample Powershell

for files use it like this

if(test-path "C:\Users\Codesnippets\Desktop\Test.xml") 
{ 
	echo "exists!" 
} 
else 
{ 
	echo "not exists!" 
}

for folders/directories use it like this

if(test-path "C:\Users\Codesnippets\Desktop") 
{ 
	echo "exists!" 
} 
else 
{ 
	echo "not exists!" 
}

Please Share us:
Please rate this snippet

2 thoughts on “How to check if file or folder exists in Powershell

Leave a Reply