Create Folder on remote server


I have a problem with a new-item command that i issued to create a folder on a remote server.
Running the below command on local server works without a problem. Error rose when trying the same with a remote (ping-able) server. As a side note: I’m able to access the remote server (SRV2) file system drive with Get-Item/ChildItem cmdlets. Here’s a snapshot of the problematic line in the function:

$bic = "random input ID"
$Slave = 'SRV2'
$Lspath2 = 'D:\somefolder'
$dir = New-Item -ItemType directory -Path ("\\$Slave\$LSpath2\trax_" + $bic)

I know that Invoke-Command can be used to that extend, but as you can see i want to be able to grab the created folder into a variable which will be used further in the function.

Any hints, direction, or rethinking of what i’m trying to accomplish will be much appreciated.

Many thanks!!!

That’s because you haven’t properly formed a UNC that can access a remote server. What you have is:


That’s an illegal UNC path. What you need instead of D: is a shared folder that points to the root of D:. For example:


Assuming the default D$ administrative share is in place. Colons (:slight_smile: are not legal in UNC paths. Additionally, grabbing the folder into a variable is not likely to help you, because none of the path information in the resulting folder object will be a legal UNC. The folder will only know its local path, not any UNC-accessible paths.

This isn’t really a PowerShell thing; it’s file system basics.

Were you doing this with Invoke-Command, it would work, because the command would run ON the remote machine, and would expect local paths (D:) not UNCs. Invoke-Command will still return object information to you.

Thanks Don, for quick heads up.
It’s, indeed, a matter of file systems understanding.

From your answer i get that i incorrectly form UNC paths in the commands and that capturing it in the variable won’t help because, well, it have not been well formatted.

So, for the folder creation on a remote server that i’m trying to accomplish here, i’d better stick with Invoke-Command?

Well, if all you want to do is create the folder, it doesn’t really matter. Both will work equally well. You could also map a New-PSDrive to the root of D: (again, using a valid UNC), and by changing to the resulting drive you can treat all the paths as local from that root. Lots of ways to do it, none are necessarily better than the others.

But there shouldn’t be a need to capture a variable representing the new folder, regardless. Any further operations would still need to know the folder’s path, which you’ll know in advance anyway.

Thanks Don. Your explanation are as always simple and spot on.