dir Z: works in cmd but fails in PowerShell.
I need this for a script.
What am I doing wrong?
PowerCLI C:\> Get-ChildItem Z:
Get-ChildItem : Cannot find drive. A drive with the name 'Z' does not exist.
At line:1 char:1
+ Get-ChildItem Z:
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Z:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Related
I am trying to move 34k images from a large dir that has 500k images. I only need the ones on the csv I created. the code I wrote call the item but doesn't move them. I wrote the code below to test on my desktop before touching production images (10 images but only want 5 to move). I am new to scripting, appologies for any mistakes I made. Thank you for any help that can be provided. I have learned a lot on this site.
Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
this is the error I get:
PS C:\Windows\system32> Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
Copy-Item : A positional parameter cannot be found that accepts argument 'FullName '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument '-------- '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png'.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
here is what the csv looks like:
FullName
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png
Edited to show before & after folder contents.
Assuming your CSV file looks like this:
Source
------
apple-ABC.doc
apple-ABC.pdf
Then this short script will do the work.
$MoveList = Import-Csv -path "G:\Test\FilesToMove.csv"
For ($Cntr = 0; $Cntr -lt $MoveList.Count; $Cntr++) {
$MIArgs =
#{Path = "G:\Test\$($MoveList[$($Cntr)].Source)"
Destination = "G:\Test\Music"}
Move-Item #MIArgs
}
Note if your CSV has the paths you just need to delete the test path [G:\Test] from the Path argument. Of course you'll provide appropriate paths for all items in my example.
Folders before running code:
Folders after running code:
HTH
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
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\"
I'm trying to write a script on a Windows 2008 R2 server to change the contents of an HTM file (Statuspage.htm) from "permit_connections=yes" to "permit_connections=no". I get the following error:
Get-Process : A positional parameter cannot be found that accepts argument
'permit_connections=no '.
At line:1 char:1
+ PS C:\> ((Get-Content -path D:\inetpub\wwwroot\statuspage.htm -Raw) - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
Get-Process : A positional parameter cannot be found that accepts argument
'Get-Content'.
At line:2 char:1
+ PS C:\> Get-Content -Path (D:\inetpub\wwwroot\statuspage.htm)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
Following is my script:
PS C:\> ((Get-Content -Path D:\inetpub\wwwroot\statuspage.htm -Raw) -replace 'permit_connections=yes','permit_connections=no') | Set-Content -Path D:\inetpub\wwwroot\statuspage.htm
PS C:\> Get-Content -path D:\inetpub\wwwroot\statuspage.htm
I am new to Powershell, and am testing out the functionality of Copy-Item according to this documentation. I am trying to copy files and folders from a subfolder into it's containing folder, but it is not working. What command do I need to use?
Here is my sample file structure
C:\
Example
|
|__Top
|
|__Middle
|
|__Bottom
I tried to copy all the files and subfolders from Middle into Top using
Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recurse
but received this error
Copy-Item : An item with the specified name C:\Example\Top\Middle already exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (C:\Example\Top\Middle:String)
[Copy-Item], IO
Exception
+ FullyQualifiedErrorId :
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Cannot overwrite the item C:\Example\Top\Middle\InTheMiddle.txt
with itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError:
(C:\Example\Top\Middle\InTheMiddle.txt:String) [Co
py-Item], IOException
+ FullyQualifiedErrorId :
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : An item with the specified name C:\Example\Top\Middle\Bottom already
exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists:
(C:\Example\Top\Middle\Bottom:String) [Copy-It
em], IOException
+ FullyQualifiedErrorId :
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Cannot overwrite the item
C:\Example\Top\Middle\Bottom\InTheBottom.txt with
itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError:
(C:\Example\Top\...InTheBottom.txt:String) [Copy-I
tem], IOException
+ FullyQualifiedErrorId :
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
Powershell Version 5.1.17134.112 Running as Administrator
You trying to copy child folder to its parent folder. To copy content of "Middle" folder you need to use this command:
Copy-Item "C:\Example\Top\Middle\*" -Destination "C:\Example\Top" -Recurse
I believe this will accomplish what you are looking for as the simplest example. It would need tweaked depending on your scenario.
-Force would need applied to overwrite.
$source = "C:\Example\Top\Middle\*"
$destination = "C:\Example\Top\"
Copy-Item -Path $source -Destination $destination -Include *.*
For instance, consider the following console transcript:
PS C:\dev\windows> rmdir -Recurse .\bin
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Implementation\Common.DTO.XML: Access to the path 'Common.DTO.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Common.DTO.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug\Implementation cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (Implementation:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.Contract.XML: Access to the path 'Shunra.Common.Contract.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Shunra.Common.Contract.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.XML: Access to the path 'Shunra.Common.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Shunra.Common.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (Debug:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (DotNet:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (C:\dev\windows\bin:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
PS C:\dev\windows>
Now compare it with the ordinary shell (cmd.exe):
C:\dev\windows>rmdir /s/q bin
bin\DotNet\Debug\IMPLEM~1\Common.DTO.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.Contract.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.XML - Access is denied.
C:\dev\windows>
The difference is obvious and I like the laconicity of cmd.exe much much more than the verbosity of the powershell.
Can I have the same laconicity in powershell? If not for all the commands then maybe just for the Remove-Item, which I use often?
The closest result you can get is by changing the global $ErrorView value to "CategoryView". Another way is to create your own view.