Garrett
Garrett

Reputation: 649

Powershell to build folder structures

I am trying to automate building a folder structure daily on a server. The script works, but for some reason it's building it on my desktop instead of the server.

#get date info
$ymd = Get-Date -Format yyyy_MM_dd

#name server paths
$x = '\\server\x'

#build out the internal retouch structures for the projects
'folder1', 'folder2', 'folder3\a', 'folder3\b' |  % {New-Item -Name "$x\$ymd\$_" -ItemType 'Directory'}

Even if I type the server path out it builds it on my desktop:

#get date info
$ymd = Get-Date -Format yyyy_MM_dd

#name server paths
$x = '\\server\x'

#build out the internal retouch structures for the projects
'folder1', 'folder2', 'folder3\a', 'folder3\b' |  % {New-Item -Name "\\server\x\$ymd\$_" -ItemType 'Directory'}

The path that it's building it is not \\server\x\$ymd\... it's C:\Users\username\Desktop\server\x\$ymd\...

This happens if I run it in ISE or right click and run with powershell. How do I tell it to use the server path literally and not build it in the local environment? I have no issues accessing the server and use powershell on the server everyday, I haven't tried making the folder structure like this before so I assume I am overlooking something simple in the code.

Upvotes: 0

Views: 139

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 61293

The problem is that when you don't use -Path the current location is used as default. This is what MS Docs for this cmdlet says:

"... The default is the current location when Path is omitted ..."

Also, it's good to know that -Path is the parameter at position 0 for this function. If you put the path without using any argument you wouldn't have had this problem :)

Upvotes: 1

Related Questions