file rename in bulk in powershell using foreach loop - powershell

$folderpath = 'E:\BOOKS\Python\python\python'
$items = Get-ChildItem -Recurse $folderpathc *_pdf
foreach( $i in $items) { Rename-Item E:\BOOKS\Python\python\python\$i E:\BOOKS\Python\python\python\$i.pdf }
Hi, I tried to rename the file under a folder using above command but not able to do and got below error.
Rename-Item : Cannot rename because item at 'E:\BOOKS\Python\python\python\book_pdf' does not exist.
At line:1 char:37
+ foreach( $i in $items) { Rename-Item <<<< E:\BOOKS\Python\python\python\$i E:\BOOKS\Python\python\python\$i.pdf }
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

Looks like you want to change all '_pdf' to '.pdf', if so this is a pretty easy way to do it.
ls -Path 'E:\BOOKS\Python\python\python' -Filter *_pdf |
ForEach-Object {$_ | Rename-Item -NewName $_.Name.Replace('_pdf', '.pdf')}

You're over complicating things. Don't re-type out the path name to the file, use the FullName property already provided by the Get-ChildItem cmdlet. Then just use a substring of the BaseName property to remove the last 4 characters, and add ".pdf" to the end.
$folderpath = 'E:\BOOKS\Python\python\python'
$items = Get-ChildItem -Recurse $folderpathc *_pdf
foreach( $i in $items) {
Rename-Item $i.FullName ($i.basename.substring(0,$i.BaseName.length-4)+".pdf")
}

Related

How do I remove a leading or trailing blank space in file name with PowerShell?

I'm basically trying to trim() any filenames that have a leading or trailing space at the end of the name. This is the code I've got so far
$foldersToCheck = "$env:userprofile\documents", "$env:userprofile\pictures", "$env:userprofile\desktop"
foreach ($folder in $foldersToCheck) {
get-childitem -path $folder -recurse | foreach-object {
if ($_.name.startswith(" ") -or $_.name.endswith(" ")) {
$newName = $_.name.trim()
rename-item -path $_ -newName $newname
}
}
}
If I create a test file (c:\users\someusername\desktop\ test.txt), then I receive this error
rename-item : Cannot rename because item at ' test.txt' does not exist.
At line:6 char:13
+ rename-item -path $_ -newName $newname
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
So, it looks like it found the file that needs to be renamed, but then says it doesnt exist.
The problem here is that PowerShell resolves $_ to just the file name when attempting to convert it to a string it can bind to -Path.
Explicitly pass the full path of the file and it'll work:
Rename-Item -Path $_.FullName -NewName $newname
Alternatively, pipe the $_ file object to Rename-Item and PowerShell will automatically figure out that it needs to bind $_.FullName to Rename-Item's -LiteralPath parameter:
$_ |Rename-Item -NewName $newname
You can also turn the whole loop into a single pipeline, and then take advantage of a pipeline-bound expression against -NewName:
$foldersToCheck |Get-ChildItem -Recurse |Rename-Item -NewName { $_.Name.Trim() }
If the existing Name and the -NewName values are the same, Rename-Item will just leave the files alone anyway :)

How to move and rename multiple files using powershell?

I have a large number of files (>4 million) and need to rename and move them to another folder in small steps.
I am a total beginner in powershell, but I already managed to move them in small packages of 100 files (the powershell script is executed as scheduled task).
But so far I failed with renaming the files. In each file, there are two strings that need to be replaced.
The following codes works fine, except for the renaming part (line 12 and 13):
#Get 'n' number of files
$FileLimit = 100
$PickupDirectory = Get-ChildItem -Path "\\server\path$\ERROR\subfolder\"
$DropDirectory = "\\server\path$\destination\"
$Counter = 0
foreach ($file in $PickupDirectory)
{
if ($Counter -ne $FileLimit)
{
$file | Rename-Item -NewName {$_.name -replace '999999','367'}
$file | Rename-Item -NewName {$_.name -replace 'oldname','newname'}
$Destination = $DropDirectory+$file.Name
Move-Item $file.FullName -destination $Destination
$Counter++
}
}
exit
What is the correct way to rename those files?
Thank you so much for your help!
Best wishes
Philipp
Edit: Sorry, here's the Error Log :
Rename-Item : Cannot rename because item at 'Microsoft.PowerShell.Core\FileSystem::\\server\path$\ERROR\subfolder\1566392#5990762$20180116^999999_2018_01_16_oldname_1566392_Kägi.pdf' does not exist.
At C:\Scripts\mv_Verordnung_für_Physiotherapie.ps1:12 char:28
+ ... pDirectory | Rename-Item -NewName {$_.name -replace '^999999','^367'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Edit2: Updated the code with tipps from comments. Error still the same.
You should do the limiting (if necessary at all) earlier with the Get-ChildItem =>
$PickupDirectory = Get-ChildItem -Path "\\server\path$\ERROR\subfolder\" | Select -First $FileLimit
Instead of using the currently iterated item ($file) you use the whole array $PickupDirectory
You can't apply the 2. replace to an already changed value.
instead of rename and move do it in one step.
#Get 'n' number of files
$FileLimit = 100
$PickupDirectory = Get-ChildItem -Path "\\server\path$\ERROR\subfolder\" | Select -First $FileLimit
$DropDirectory = "\\server\path$\destination\"
foreach ($file in $PickupDirectory){
$Destination = Join-Path $DropDirectory ($file.Name -replace '^999999','^367' `
-replace 'oldname','newname')
$file | Move-Item -Destination $Destination
}
You're calling Rename-Item on the $PickupDirectory collection. Instead call it on the $file variable you are using in the foreach loop:
$file | Rename-Item -NewName { $_.name -replace '^999999', '^367' }
$file | Rename-Item -NewName { $_.name -replace 'oldname', 'newname' }

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory' [duplicate]

This question already has answers here:
A parameter cannot be found that matches parameter name 'directory'
(2 answers)
Closed last year.
I am getting Get-ChildItem : *A parameter cannot be found that matches parameter name 'Directory'* error message , below is the script used, kindly help me in finding the issue?
Script:
Function Clear-ReadOnlyFiles([string]$path)
{
"Clearing readonly attribute from files in $path"
New-Variable -Name read_only -Value 1 -Option readonly
Get-ChildItem -Path $path |
Where-Object { $_.attributes -match 'readonly' } |
ForEach-Object {
$_.attributes = $_.attributes -Bxor $read_only }
}#end Clear-ReadonlyFiles
$ZipCmd = "{0}\7za.exe" -f $BackupDir
$ZipCmdArgs = "-sdel"
# Grab Directories in Location
$Directories = Get-ChildItem -Path $BackupDir -Directory
# Cycle Through and Launch 7za to Compress
# Check if Archive Exists - If yes, delete source folder
ForEach ($Directory in $Directories)
{
$Source = "{0}\{1}" -f $BackupDir,$Directory.Name
$Archive = "{0}.7z" -f $Source
# Clear Read-Only Attribute on Folder
$SubFiles = Get-ChildItem -Path $Directory -Recurse -ReadOnly
ForEach ($SubFile in $SubFiles) {
$SubFile.IsReadOnly = $False
}
#If ($Directory.IsReadOnly -eq $True) { $Directory.set_IsReadOnly($False) }
$AllArgs = #('a', $ZipCmdArgs, $Archive, $Source);
& $ZipCmd $AllArgs
}
Error message:
Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At C:\Play\BackupCompress.ps1:35 char:57
+ $Directories = Get-ChildItem -Path $BackupDir -Directory <<<<
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : A parameter cannot be found that matches parameter name 'ReadOnly'.
At C:\Play\BackupCompress.ps1:45 char:66
+ $SubFiles = Get-ChildItem -Path $Directory -Recurse -ReadOnly <<<<
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Property 'IsReadOnly' cannot be found on this object; make sure it exists and is settable.
At C:\Play\BackupCompress.ps1:47 char:18
+ $SubFile. <<<< IsReadOnly = $False
+ CategoryInfo : InvalidOperation: (IsReadOnly:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
I believe that the Directory switch was added in PowerShell 3.0. In older versions you will need to check the PSIsContainer property of each child item, which will be true if the item is a directory:
$Directories = Get-ChildItem -Path $BackupDir | Where-Object { $_.PSIsContainer }
-Directory is a conditional parameter and only works on paths whose provider is "FileSystem". It's present on this page: Get-ChildItem for FileSystem, but not on the generic one.
Make sure that the correct provider appears when you type the following:
Get-Item $BackupDir | Select-Object -Property PSProvider

Get-ChildItem: Illegal characters in path

I'm trying to login to multiple servers and then get list of files from those servers.
Below is my script:
$ServerName=Get-content "D:\HOMEWARE\BLRMorningCheck\Jerry\servername.txt"
foreach ($server in $ServerName)
{
$server_host=echo $server | %{$data = $_.split(";"); Write-Output "$($data[0])"}
$Targetfolder=echo $server | %{$data = $_.split(";"); Write-Output "$($data[1])"}
$Extension =#("*.log","*.txt*")
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse
echo $Files
}
When I run I debug mode I see that it really doesnt pick the files.
Error:
Get-ChildItem : Illegal characters in path.
At D:\HOMEWARE\BLRMorningCheck\Jerry\test.ps1:14 char:23
+ $Files = Get-Childitem <<<< $TargetFolder -Include $Extension -Recurse
+ CategoryInfo : InvalidArgument: (D:\HOMEWARE\BLR...ck\Jerry\Check":String) [Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : Cannot find path 'D:\HOMEWARE\BLRMorningCheck\Jerry\"\srvparrtfh01\d$\HOMEWARE\BLRMorningCheck\Jerry\Check"' because it does not exist.
At D:\HOMEWARE\BLRMorningCheck\Jerry\test.ps1:14 char:23
+ $Files = Get-Childitem <<<< $TargetFolder -Include $Extension -Recurse
+ CategoryInfo : ObjectNotFound: (D:\HOMEWARE\BLR...ck\Jerry\Check":String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Note: If I run it manually, example
$Files = Get-Childitem \\servername\d$\HOMEWARE\BLRMorningCheck\Jerry\Check -Include "*.log","*.txt*" -Recurse
I get the output.
As #arco444 and #PetSerAl mentioned in the comments: your path string has leading/trailing double quotes, which are invalid characters in a path. You need to remove them to make the path work as intended:
$Files = Get-Childitem ($TargetFolder -replace '^"|"$') -Include $Extension -Recurse
Of course, since your input file seems to be a some sort of CSV, you could use Import-Csv instead of reading the file with Get-Content and manually splitting the fields. That would already take care of the double quotes on file import.
$filename = 'D:\HOMEWARE\BLRMorningCheck\Jerry\servername.txt'
$extension = '*.log', '*.txt*'
Import-Csv $filename -Delimiter ';' -Header 'ComputerName', 'TargetFolder' | % {
Get-Childitem $_.TargetFolder -Include $extension -Recurse
}
This was an absolute nightmare, get-childitem cannot except a string variable if there are multiple paths.
Get-ChildItem -Path "\server1\c$\temp", "\server1\d$\temp" -File
-recurse # works
$path = '"\server1\c$\temp", "\server1\d$\temp"'; Get-ChildItem -Path "\server1\c$\temp", "\server1\d$\temp" -File
-recurse # fails with cannot find path
$path = "'"\server1\c$\temp", "\server1\d$\temp'""; Get-ChildItem -Path "\server1\c$\temp", "\server1\d$\temp" -File
-recurse # fails with illegal character message (the tick)
Any string that has multiple paths fails, however an array with += as shown below will work.
$servers = #("server1", "server2");
$partialPaths = #("\c$\temp\", "\d$\temp\");
foreach ($server in $servers)
{
$paths = #();
foreach ($partialPath in $partialPaths)
{
$paths += "\\" + $server + $partialPath;
}
}
Get-ChildItem -Path $paths -File -recurse;

Get data from CSV to delete users folders from multiple servers

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
}