Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: How to check for multiple conditions (folder existence)

Tags:

powershell

I am in the process of writing a script to make changes to folder permissions. Before it does that I would to do some checking to make sure that I am working in the correct directory. My problem is how do I check to see if four subfolders (i.e. Admin, Workspace, Com, & Data) exists before the script progresses. I assume I would be using Test-Path on each directory.

like image 489
pizzim13 Avatar asked Oct 14 '10 18:10

pizzim13


People also ask

How do you check if a folder exists or not in PowerShell?

The Test-Path Cmdlet $Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.

How do you create a directory if it doesn't exist in PowerShell?

PowerShell Create Directory If Not Exists using Test-Path If a directory exists then it will return $True. If a path or directory is missing or doesn't exist, it will return $False. Using PowerShell New-Item cmdlet, it will create directory if not exists using Test-Path.

How do I get the file path in PowerShell?

To get the full path of the file in PowerShell, use the Get-ChildItem to get files in the directory and pass the output to foreach-object to iterate over the file and get the full name of the file.

What is mkdir in PowerShell?

PowerShell mkdir is a function defined in PowerShell to create directory and it's an alias of md command. PowerShell mkdir. PowerShell mkdir uses New-Item cmdlet to create directory and has the same syntax as PowerShell New-Item cmdlet.


1 Answers

What's wrong with the following?

if ( (Test-Path $path1) -and (Test-Path $path2) ) { 

}
like image 190
JaredPar Avatar answered Oct 13 '22 21:10

JaredPar