Creating new folder on remote system

I’m trying to copy a file to a new folder on a remote server but its creating the new folder on my computer, not on the server. NEED HELP!

This is what I have:

$AO = New-PSSession -ComputerName “computer” -Credential “user”
Copy-Item -Path “C:\Test1.txt” -Destination (New-Item -Path “C:\Tools\prac” -ItemType “directory”) -Force -ToSession $AO

Try this:

New-Item -ItemType Directory -Path ‘\computer\c$\tools\prac’

Not sure if you are using the PSSession for other things but it is not necessary here if you have admin on the remote system.

1 Like

It works!! thanks!! So for the “-Path” I need to include the computer name and the directory is always spelled with a capital D and not “” ?

No, when you tab complete it makes it a capital d. It’s not required but it does look better imo. The single quotes prevent the dollar sign from being interpreted as a variable. In this case it isn’t required because it’s not followed by a number or letter, it’s a good idea to get in the habit of only using double quotes when you need variable expansion.

I know this has been answered, but I do this with every server I build to add a file based on the server type. I don’t use a PSSession, this a snippet of the needed code.

Computers = Get-Content C:\Powershell\Text\servers.txt
Import-Module ActiveDirectory
Clear-Host

foreach ($Computer in $Computers)
{

$destinationFolder = “\$computer\C$\Temp”

if (!(Test-Path -path $destinationFolder))
{
New-Item $destinationFolder -Type Directory
}

1 Like

One other point of note that I really like about New-Item is that it will create the full path you define if it does not exist :slight_smile:

Thank You so much everyone. I’m teaching myself powershell for work so I’m still learning the correct syntax and stuff. :slight_smile: