Using PowerShell - Create a folder if not exists

Using PowerShell create a folder if not exists – in 2 proven steps

No comments

Loading

Using PowerShell create a folder if not exists – in this post, I will show how to create a folder or directory if not exist in the given directory or path using PowerShell code or script.

The below PowerShell code will create a folder with the name “LogFiles” in the .ps1 file root directory if not exists.

Using PowerShell create a folder if not exists – create a folder in the Powershell .ps1 file directory if the folder does not exist.

cls
$fileName = "File_Uploading_Report"
#'yyyyMMddhhmm   yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename =  $enddate + '_VMReport.doc'  
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"   
$invocation = (Get-Variable MyInvocation).Value  
$directoryPath = Split-Path $invocation.MyCommand.Path  

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))  
{  
 New-Item -ItemType directory -Path $directoryPathForLog
 Write-Host "Folder path has been created successfully at: " $directoryPathForLog    
 }
else 
{ 
Write-Host "The given folder path $directoryPathForLog already exists"; 
}


Folder creation example in the Powershell .ps1 file directory: Using PowerShell create a folder if not exists

Using PowerShell create a folder if not exists demo

Create a folder in the given directory if the folder or directory not exists – using PowerShell create a folder if not exists

Using the below code we can create a folder in a given path or directory if the folder does not exist. 

cls
$directoyPath="C:\Temp\CreateFolderIfNotExists\Sample Path\Test Folder";
if(!(Test-Path -path $directoyPath))  
{  
    New-Item -ItemType directory -Path $directoyPath
    Write-Host "Folder path has been created successfully at: " $directoyPath
               
}
else
{
Write-Host "The given folder path $directoyPath already exists";
}

Create a folder in the given directory if the folder or directory does not exist – Example: Using PowerShell create a folder if not exists

Using PowerShell create a folder if not exists example

Summary: Using PowerShell create a folder if not exists

Thus, in this post we have explored the below:

  • Create a Folder If Not Exists in PowerShell.
  • Create a directory if it does not exist.
  • New-Item – Create Folders & Files using PowerShell Basics.
  • PowerShell script to validate if a folder exists – creates it if not.
  • Test-Path – Folder Creation in PowerShell.
  • Using PowerShell check and create a folder if it doesn’t exist.

See Also: PowerShell tutorial for beginners

You may also like the below PowerShell tutorials:

Looking for a PowerApps tutorial from the scratch? Then you are in the right place, below are a bunch PowerApps articles you may like:

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:


Buy SharePoint Online & Office 365 Administration eBook

About Post Author

Do you have a better solution or question on this topic? Please leave a comment