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 }
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.
Leave a Reply Cancel reply