1

I'm a Powershell novice. I've written a .ps1 script to do a number of things, all of which execute perfectly, except when it comes to a "Copy-Item" command, which I'm using to copy contents of the (Windows 10) desktop to another folder.

It's failing at the hyphen in "Copy-Item". I have a feeling I'm either missing a "*" in one of the paths. or have one where it should not be. I've tried it with various combinations, but no joy.

For test purposes, I have 3 items on the desktop: 1) a folder shortcut, 2) a Powershell script shortcut, and 3) A GodMode 'folder'.

Thank you in advance for your help. Following is a subset of the code:

$env:path += ";D:\PowershellScriptsFolder" #   this is the location of my ps1

# (a bunch of code here, all of which runs fine) #

# Desktop
    # Define Variables
        $DestinationPath = "D:\folder1\subfolder\*"
        $SourcePathRoot = 'C:\Users\Sfrn\Desktop\' #   this is the location of my Win 10 desktop
    #
    Remove-Item -Recurse -Path $DestinationPath -Force

(*** Here's where it fails: ***)

Copy-Item -Recurse -Path $SourcePathRoot -Destination $DestinationPath -Force #   this is line 32

(*** Here's the error output - character 5 is the hyphen in "Copy-Item": ***)

Copy-Item : Illegal characters in path. At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5

  • Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (D:\Dropbox\DB_AppData\Desktop*:String) [Copy-Item], ArgumentException
    • FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Illegal characters in path. At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5

  • Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (desktop.ini:FileInfo) [Copy-Item], ArgumentException
    • FullyQualifiedErrorId : CopyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Illegal characters in path. At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5

  • Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (Dropbox.lnk:FileInfo) [Copy-Item], ArgumentException
    • FullyQualifiedErrorId : CopyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Illegal characters in path. At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5

  • Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (OpenApps.lnk:FileInfo) [Copy-Item], ArgumentException
    • FullyQualifiedErrorId : CopyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Illegal characters in path. At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5

  • Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (D:\Dropbox\DB_AppData\Desktop*:String) [Copy-Item], ArgumentException
    • FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand

(*** End of error output: ***)

(a bunch of code continues to run here, all of which is fine)

END

1 Answer 1

2

You have an asterisk in the wrong spot. It needs to be in the source directory path, not the destination, like this:

$SourcePathRoot = "C:\Users\Sfrn\Desktop\*"
$DestinationPath = "D:\folder1\subfolder\"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Catherine! Perfect!
I upvoted your answer, but I apparently don't have enough points for it to count. :-(
You're welcome, glad I was able to help. As the question author, you should be able to mark it as an accepted answer. I don't think that is blocked by reputation.

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.