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
}
Related
After running this script:
# Input path
$input_path = 'C:\Users\larry\Pictures\2003'
# Export file
$documents_export_file = 'C:\Users\larry\Documents\Temp\ffmpeg\fullnames.csv'
# Copy-to folder
$copy_to = "C:\Users\larry\Documents\Temp\ffmpeg\movies"
# List filenames in $documents_export_file
Get-ChildItem -Attributes !Directory -Path $input_path -Recurse -force | select-object -Property fullName | Export-Csv $documents_export_file
# Copy the files listed in $documents_export_file to $copy_to folder
get-content $documents_export_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
I get the following error:
copy-item : Cannot find drive. A drive with the name '"C' does not exist.
At line:18 char:55
+ ... ort_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
copy-item : Cannot find drive. A drive with the name '"C' does not exist.
At line:18 char:55
+ ... ort_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
As far as I can figure out, it has something to do with my Export-Csv, bur I canĀ“t figure it out.
Please give me a hand.
There's no need to export the list of files to CSV in the first place - assign the output from Get-ChildItem to a variable instead, and then pipe the contents directly to Copy-Item:
$files = Get-ChildItem -Attributes !Directory -Path $input_path -Recurse -Force
$files |Copy-Item -Destination $copy_to
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 am very new to powershell and situation is that I have some unneeded users folder that must be deleted on different servers. Path on all servers is f.e. "\server1\hiddenshare$\username" My CSV looks like:
ServerName,FolderName
SERVER1,AAAA
SERVER2,AAA1
SERVER3,AAA2
And I am trying to run this code:
$path = "C:\temp\servers_folders.csv"
$ServerName = Import-Csv -Path $path
$FolderName = Import-CSV -Path $path
Import-Csv -Path $path | % {
Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
}
After all I get this error:
Remove-Item : Cannot find path '\\ \$\ ' because it does not exist.
At C:\temp\servers_folders.csv.ps1:5 char:3
+ Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\ \usr$\ :String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
you dont need to import your csv more than once.
Inside the foreach block you have to reference the current item by $_
try this, remove -whatif if it's ok
$servers=import-csv .\servers.csv
$servers | %{
remove-item -path \\$($_.servername)\users$\$($_.FolderName) -whatif
}