TECHDIIP

  • Tech
  • Windows
  • Powershell
  • Scripts
  • News
  • Games

Powershell create folder if it does not exist

27 October 2020 admin Leave a Comment

Powershell create folder if it does not exist on any windows computer, there is a simple code that we need to put in your script to create a folder when it doesn’t exist, here the code follows.

When we have a requirement to create a folder on single or multiple computers when it doesn’t exist, then here is the simple Powershell code that creates a folder.

Note: This script creates a folder if it doesn’t exist,  it does not override if a folder already exists.

$path = "C:\temp\NewFolder"

If(!(test-path $path))
{
      New-Item -ItemType Directory -Force -Path $path 
}

powershell create a folder

Create a Folder on Multiple Computers.

let’s see how we can write the code to create a folder on multiple computers.

When you have a requirement to Copy files on multiple remote machines with a new folder then we need to write the script this way.

We need to create a file for storing all the servers or multiple machines list, I named itas Allservers.txt for and saved it in my local machine c:\temp\Allservers.txt

$Allserers = get-content -path "C:\temp\Allservers.txt"

 

foreach ($server in $allservers)
{

$path = “\\$servver\C$\temp\NewFolder”
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path
copy-item “\\mysserver\data” -destination “\\$servver\C$\temp\NewFolder”
}
}

You May Also interested in reading…

1.     Powershell-top-10-commands
2.     5-Best free Youtube video to audio converters in 2020
3. Top-5-notepad-tricks-that-you-should-know-in-2021
4.     Zoom-Testing

Thank you for reading this article, if you have any questions please let us know.

Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.

Scripts

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Techdiip © 2012 - 2020 | Please Read Our

  • Privacy Policy
  • Disclaimer – User Agreement
  • About Us
  • Contact Us