0

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.

5
  • 7
    This is because you're using -Name instead of -Path :) Commented Apr 9, 2021 at 21:21
  • Submit this as an answer and i'll mark it correct. Commented Apr 12, 2021 at 13:57
  • There, I added an answer. Glad it worked :) Commented Apr 12, 2021 at 14:13
  • @SantiagoSquarzon if you submit your reply as an answer I'll mark it correct. Commented Apr 13, 2021 at 15:03
  • I already did, I think Commented Apr 13, 2021 at 15:12

1 Answer 1

1

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

@Garrett No problem, glad it worked for you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.