When recursively searching through a specific directory, Get-ChildItem will search starting at the root. I do not want to search through the C:\Windows directory. I want to restrict the search to the C:\Docs directory.
Here is what I am running:
PS> Get-ChildItem -path “C:\docs” -Filter "*crypt*" -recurse -ErrorAction Stop
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
At line:1 char:1
+ Get-ChildItem -path “C:\docs” -Filter "*crypt*" -recurse -ErrorAction ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CSC:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
PS> Get-ChildItem -path “C:\docs” -Filter "*crypt*" -exclude "C:\windows" -recurse -ErrorAction Stop
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
At line:1 char:1
+ Get-ChildItem -path “C:\docs” -Filter "*crypt*" -exclude "C:\windows" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CSC:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
EDIT: TL;DR: User error. I do not have the C:\Docs directory.
I am editing this script, which is running on several servers. I am testing it on my laptop. I still don't understand why it would look through the rest of the file system, once it could not find the starting path.
It looks like (I haven't searched enough) this may be a bug in Get-ChildItem; if you pass a non-existent path to the -path parameter, it searches from the root of that drive (at least for local drives).
Before calling Get-ChildItem, test for the existence of the path and you can avoid this.
$mypath = "c:\docs";
if (test-path -path $mypath) {
Get-childitem –path $mypath –filter “*crypt*" -recurse -ErrorAction stop;
} else {
Write-Warning "$mypath not found";
}
Related
In GNU/Linux, I can just do this:
tar cfz hierarchy.tgz hierarchy
and it just works, even if some of the items in hierarchy are being used by other processes.
In Windows, I can right-click on a folder and send it to a compressed zip file, so long as it's in another place, like the Desktop, and that works.
But in Windows PowerShell, something you would think would be so simple, isn't:
PS E:\> Compress-Archive -Path e:\lib -DestinationPath .\e-lib-all.zip
ZipArchiveHelper : The process cannot access the file
'E:\lib\company\data\data-azure-java\1.0.1\data-azure-java-1.0.1.jar' because it is being used by another process.
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:697
char:30
+ ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (E:\lib\company...-java-1.0.1.jar:String) [Write-Error], IOException
+ FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper
New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:808
char:38
+ ... $srcStream = New-Object System.IO.BinaryReader $currentFileStream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I asked a coworker, what's the PowerShell equivalent of the GUI way that just works? He thought what the GUI does in the background is create a copy using service level rather than user permissions, and then zips that.
Is there a PowerShell way to do this? I've tried some other things that did not work, namely:
Copy-Item -Path "E:\lib" -Force -PassThru | `
Get-ChildItem | `
Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Get-ChildItem -Recurse -Path $sourcePath | Compress-Archive -DestinationPath $destinationPath
Get-ChildItem -Recurse -Path "E:\lib" | Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Yes, I looked at previous SO post Compress-Archive error: PermissionDenied but it didn't help (I got the same errors).
I want copy/paste all files and all folder in other path.
May data is:
temp
Deploiement
foo.xml
bar.xml
Documentation
brute
I read official documentation on https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.1 and I find this example:
Example 3: Copy directory and contents to a new directory
Copy-Item -Path "C:\Logfiles\*" -Destination "C:\Drawings\Logs" -Recurse
In my Jenkins, I write this:
Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" -Recurse
Console error:
Copy-Item : Container cannot be copied onto existing leaf item.
At D:\JenkinsWks\workspace\pic\temp\Scripts\packer.ps1:114 char:2
+ Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (D:\JenkinsWks\w...t\Documentation:String) [Copy-Item], PSArgumentException
+ FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Container cannot be copied onto existing leaf item.
At D:\JenkinsWks\workspace\pic\temp\Scripts\packer.ps1:114 char:2
+ Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (D:\JenkinsWks\w...nt\brute:String) [Copy-Item], PSArgumentException
+ FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand
if (-Not (Test-Path "Deploy")) {
md -path "Deploy"
}
Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" -Recurse
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\"
This powershell snippet should display size and name of the files in directory on shared drive,
working fine with one path raising error with other path
copied two logs which raise error(log2) and other worked fine(log1)
for long path issue i tried like $joined_path2=Join-Path -LiteralPath '\\?\UNC\' -ChildPath $joined_path
$check_dir_path=\\XXXXXX.yy.NN.com\qeshare\Environments #(not working)
#check_dir_path=\\XXXXXX.yy.NN.com\qeshare\Environments\Private\ccccc #(working fine with this path)
$folders_in_dir=Get-ChildItem $check_dir_path -directory | Foreach-Object {$_.Name}
foreach($folder in $folders_in_dir){
$joined_path=Join-Path -Path $check_dir_path -ChildPath $folder
#$joined_path2=Join-Path -Path '\\?\UNC\' -ChildPath $joined_path(tried not working)
#write-host $joined_path2
$files_sub = Get-ChildItem -Path $joined_path -Recurse
if($files_sub.count -gt 0){
$size_dir = ($files_sub | Measure-Object -Sum Length).Sum
Write-Host "$size_dir_padded $folder `r`n"
}
LOG1 (no issue):
size(bytes) file name
738621 PICK_PATH
2296972 TaskRand-Emailbuild
43961103 Task9-changetoTM1
11846548 Task10-linuxlogin
LOG2 (issue):
size(bytes) file name
62945815 BASS2 Rebase
24696625 JAZN-April04
Error messages:
Get-ChildItem : The given path's format is not supported.
At C:\BBBB_task\file_size_check.ps1:28 char:
+ $files_sub = Get-ChildItem -Path $joined_path -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.GetChildItemCommand
: ReadError: (\\slcnas463.us....rea\ADS30207754:String) [Get-ChildItem], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At C:\gopala_task\file_size_check.ps1:28 char:14
+ $files_sub = Get-ChildItem -Path $joined_path -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo
i have to delete folders based on a excel list, so i have tried to import the execl with import csv but it dosent work.
The import doesnt fill the variable in the correct format.
This is the code i tried to use:
$folders = import-csv G:\Book1.csv foreach ($folder in $folders) {Remove-Item -Path $folder -Recurse -Force}
The error is this:
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
In the CSV in the 1.Line heading stands foldername in line 2-4 are the name of the folders (a, b, c).
This is the CSV:
Foldername
G:\Test\a
G:\Test\b
G:\Test\c
The directory is G:\Test\a , b, c
I have found a Solution, the CSV file was a Problem and the import so i had to modify the CSV File:
"Foldername"
"G:\Test\a"
"G:\Test\b"
"G:\Test\c"
the powershell code is this now:
$folders = import-csv .csv | ForEach-Object {
write-host $($_.Foldername)
Remove-Item -Path $($_.Foldername) -Recurse -Force
}