Creating new folder on remote system

Welcome Forums General PowerShell Q&A Creating new folder on remote system

Viewing 6 reply threads
  • Author
    Posts
    • #270811
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      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

      • This topic was modified 3 weeks ago by ArtOli67.
    • #270817
      Participant
      Topics: 9
      Replies: 170
      Points: 836
      Helping Hand
      Rank: Major Contributor

      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.

    • #270841
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      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 “” ?

      • This reply was modified 3 weeks ago by ArtOli67.
    • #270877
      Participant
      Topics: 9
      Replies: 677
      Points: 2,680
      Helping Hand
      Rank: Community Hero

      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.

    • #270886
      Participant
      Topics: 0
      Replies: 1
      Points: 54
      Rank: Member

      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
      }

    • #271132
      Participant
      Topics: 9
      Replies: 170
      Points: 836
      Helping Hand
      Rank: Major Contributor

      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 🙂

    • #271141
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

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

Viewing 6 reply threads
  • You must be logged in to reply to this topic.