powershell scripting help

I have a script that works, but I am trying to make it re-usable.
I needed to create the same folder in the same path, on many servers, so I used powershell, and this works fine:

$Servers='server1','server2','server3'
$NewFolder = 'Software'
$Servers | foreach { New-Item "\\$($_)\D$\$NewFolder" -ItemType Dir }
So that worked fine, but the helpdesk guys do this routinely and like I said, I wanted it to be re-useable, so I wrote this:

$servers = Read-Host "Please enter system name in this format server1"
$newfolder = Read-Host "Please enter Folder name in this format FolderName"
$path = Read-host "Please enter the path where the new folder will be, in this format D$ (If the new folder is at the root
of the D:\drive. If the folder is to be inside another folder, then that path is needed,  such as D$\tier1\"
$Servers | foreach { New-Item "\\$($_)\$path\$NewFolder" -ItemType Dir }

This works fine, but only for one system, I tried putting them in with commas, with single quotes, it keeps using the entire line
 WriteError: (\\server1...erver2\D$\Data:String) [New-Item], IOException

So how what do I need to do for this to work?
Thanks!
LVL 3
SquigglyMonkeyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian MurphyIT ArchitectCommented:
Quick way perhaps, use a servers.txt list

Pull from that or use a variable you can pass on the command line.

You could do a FOR IF but I cannot tell if you are trying to run this remote or they are on the actual server.

In that case, a variable should work like a command line for example:  myscript.ps1 Server1

But it looks more like what you need is an "array"
SquigglyMonkeyAuthor Commented:
Okay, so I thought the same thing, and found a ton of stuff on arrays.
This asks for server names comma delimited, works great.

[array]$servers = (Read-Host "Please enter system name in this format server1 if more than 1 server, separate with a ,").split(“,”) | %{$_.trim()}
$newfolder = Read-Host "Please enter Folder name in this format FolderName"
$path = Read-host "Please enter the path where the new folder will be, in this format D$ (If the new folder is at the root of the D:\drive. If the folder is to be inside another folder, then that path is needed,  such as D$\tier1\"

$Servers | foreach { New-Item "\\$($_)\$path\$NewFolder" -ItemType Dir }
SquigglyMonkeyAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for SquigglyMonkey's comment #a41027904

for the following reason:

This is the solution to the question I asked.
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
You could use split.

1. Get the list of servers:
$servers = Read-Host "`n`nPlease enter system name(s) in this format ""server1""`nFor multiple servers, enter a comma separated list e.g. ""server1, server2, etc."""

Open in new window

2. They enter a list of servers
server1, server2, server3

Open in new window

3. We now split the server variable and take out the spaces:
$servers.Split(',').Replace(' ','') | 

Open in new window


So putting it all together:
$servers = Read-Host "`n`nPlease enter system name(s) in this format ""server1""`nFor multiple servers, enter a comma separated list e.g. ""server1, server2, etc."""
$newfolder = Read-Host "Please enter Folder name in this format FolderName"
$path = Read-host "Please enter the path where the new folder will be, in this format D$ (If the new folder is at the root of the D:\drive. If the folder is to be inside another folder, then that path is needed,  such as D$\tier1\"

$servers.Split(',').Replace(' ','') | foreach { New-Item "\\$($_)\$path\$NewFolder" -ItemType Dir }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Looks like you found split.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.