2e9b2ad3-3020-4122-ad5e-d99e6ed49b49-NEWNAME_20210127_008_Customers_02-POST.csv<\/a> (1.21 KB)<\/p>","upvoteCount":0,"datePublished":"2021-06-09T23:28:47.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/17","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"Looks like there is a typo and there is ‘get-childitem’ twice, should only be there once<\/p>","upvoteCount":0,"datePublished":"2021-06-09T23:29:50.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/18","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
You are my new best FRIEND !!!<\/p>\n
For the record this is what your patients has produced for this noob and hopefully this can help others<\/p>\n
$files = Get-ChildItem \"C:\\temp\\AMLReports\" *.csv -Recurse\n\nforeach($file in $files){\n $old = get-content $file.FullName\n $newfile = $old -replace \"oldname\",\"new_name\"\n $newfile | out-file $file.FullName -Encoding ascii -Force\n\n $newname = $file.name -replace \"old_name\",\"new_name\"\n Rename-item $file.fullname -NewName $newname\n}\n<\/code><\/pre>\nOnce again thank you this has been a life saver!!<\/p>","upvoteCount":0,"datePublished":"2021-06-09T23:45:10.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/19","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}}]}}
milesit
(MILESit)
June 9, 2021, 2:38am
1
I have a root dir that has hundreds of folders in it that are named with a legacy company name and date format, I want to rename these files within all the folders with PowerShell, I have been trying all day but no luck thus far I’m a noob to scripting, only ever normal have to do login scripts and easy stuff.
I need to maintain the date format on the files so they need to look like example new_businessname06092021.csv
Get-ChildItem –Path "C:\temp\AMLReports" -Recurse -Filter *.csv
foreach($file in Get-ChildItem $SomeFolder)
{
$newname = ([String]$File).Replace("oldbuisnessname","newbusinessname")
Rename-item -Path "C:\temp\AMLReports\Renamed" $File $newname
}
Help appreciated
3 Spice ups
Neally
(Neally)
June 9, 2021, 2:52am
2
Welcome
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…
Neally
(Neally)
June 9, 2021, 2:57am
3
how about like so?
$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
foreach($file in $files){
$newname = $file.name -replace "oldname","newname"
Rename-item $file.fullname -NewName $newname -verbose
}
Test it before you let it lose in your prod environment.
milesit
(MILESit)
June 9, 2021, 3:04am
4
How did you make that so easy!
I wish I had your brain
Now I need to open every .csv with the root dir and find and replace… dont suppose this is as easy to achieve… I have been playing with
powershell -Command "(gc INPUT.csv) -replace 'oldname', 'newname' | Out-File -encoding ASCII OUTPUT.csv"
I have know idea how to incorporate this into the script you fixed up for me, thanks heaps!
this community is amazing
Miles
Neally
(Neally)
June 9, 2021, 3:09am
5
I’d update the content first and then rename.
$files = Get-ChildItem Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
foreach($file in $files){
$old = get-content $file.FullName
$newfile = $old -replace "oldname","newname"
$newfile | out-file $file.FullName -Force
$newname = $file.name -replace "oldname","newname"
Rename-item $file.fullname -NewName $newname
}
milesit
(MILESit)
June 9, 2021, 7:16pm
6
Morning from NZ
I updated the names and run it in test, didn’t seem to do anything PowerShell just opened and closed, and didn’t appear to run any lines (Reminder I’m a complete Noob :P)
Just for clarification
I have a folder that contains hundreds of folders that have csv files in them that have been named oldbusinessname-date.csv
These files all need renamed to newbusinessname-date.csv (This Script tested working for this) Thanks!
$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
foreach($file in $files){
$newname = $file.name -replace "oldname","newname"
Rename-item $file.fullname -NewName $newname -verbose
}
My second issue is the CSV files that live within the folders have a column within the spreadsheet that is named oldbusinessname that needs renamed to newbusinessname.
I tried running this and PS just opened and closed without running any lines and no error.
$files = Get-ChildItem Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
foreach($file in $files){
$old = get-content $file.FullName
$newfile = $old -replace "oldname","newname"
$newfile | out-file $file.FullName -Force
$newname = $file.name -replace "oldname","newname"
Rename-item $file.fullname -NewName $newname
}
I do very much appreciate you taking the time to help me!
Miles
Neally
(Neally)
June 9, 2021, 7:21pm
7
if you run it just like this, powershell would not indicate if anything was done, you’d just see red if it fails.
Did it actually update stuff? It worked fine in my test
I do not understand what you mean by " closed without running any lines" ? What do expect?
to see if powershell is actually doing something you need to add ‘-verbose’
$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
foreach($file in $files){
$old = get-content $file.FullName
$newfile = $old -replace "oldname","newname"
$newfile | out-file $file.FullName -Force
write-verbose "Updated $($file.fullname)" -verbose
$newname = $file.name -replace "oldname","newname"
Rename-item $file.fullname -NewName $newname -Verbose
}
milesit
(MILESit)
June 9, 2021, 7:38pm
8
With the renaming on the csv files I could see PS running heaps of stuff with the renames as there is hundreds of them, I have tried with the Verbose and still the same thing ps just opens and closes again, the content hasn’t changed, I’m using office 365 Apps - don’t suppose I have to run ps an admin or something?
milesit
(MILESit)
June 9, 2021, 7:44pm
9
Neally
(Neally)
June 9, 2021, 7:47pm
10
the CSV should not matter.
Yes, as admin would help.
how do you run it?
$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
$files.count
if you just run this, does it get a count? does it find files? how do you run it? in the ISE? from the console? as a script?
milesit
(MILESit)
June 9, 2021, 10:51pm
11
The same thing happens with that, just opens and closes.
I create a new file.ps1 then right click on it to edit paste the code in, save, then close, then right click on file and run with PowerShell
I’m wanting to run script as a daily scheduled task.
I’m not sure how to run as the file as admin, no shift right click options on the file
I get the attached with run from within the ps console
Neally
(Neally)
June 9, 2021, 10:55pm
12
you open the powershell console as admin and then just call the script.
looks like you have also execution policy restriction.
Learn all about PowerShell execution policies, various scopes, contexts, how to use Get-ExecutionPolicy and Set-ExecutionPolicy.
in task scheduler you’d call it like so
powershell.exe -executionpolicy bypass -file "c:\scripts\script.ps1"
milesit
(MILESit)
June 9, 2021, 10:57pm
13
It works if I run PS as admin I get the file count, ill test now running the rename files
ITS working! running as admin helped however it has lost the formatting almost looks like it has comer separated and taken out the columns, does it need the encoding ASCII added somehow?
Neally
(Neally)
June 9, 2021, 11:07pm
14
MILESit:
It works if I run PS as admin I get the file count, ill test now running the rename files
ITS working! running as admin helped however it has lost the formatting almost looks like it has comer separated and taken out the columns, does it need the encoding ASCII added somehow?
the encoding and such depends on your CSV files.
out-file has a few build in encoding option:
in my test it seemed to work fine
$csv = get-content ".\Input.csv"
$new = $csv -replace "dumbo","neally"
$new | out-file ".\Input2.csv"
Neally
(Neally)
June 9, 2021, 11:11pm
15
yeah so the default encoding
→ windows powershell (5.1) is unicode with BOM. (utf-16)
→ powershell (7.x) is utf8NoBOM
so try like so
out-file $file.FullName -Encoding ascii -Force
milesit
(MILESit)
June 9, 2021, 11:28pm
16
On the working test I have just noticed that It did put this at the top not sure if that’s related.
Get-ChildItem : A positional parameter cannot be found that accepts argument '*.csv'.
At line:1 char:10
+ $files = Get-ChildItem Get-ChildItem "C:\temp\AMLReports" *.csv -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
I have attached the before file and after file for reference
dd8dac5e-8056-447e-91a7-087134f5c811-OLDNAME_20210127_008_Customers_02-_PRIOR.csv (846 Bytes)
milesit
(MILESit)
June 9, 2021, 11:28pm
17
Neally
(Neally)
June 9, 2021, 11:29pm
18
Looks like there is a typo and there is ‘get-childitem’ twice, should only be there once
milesit
(MILESit)
June 9, 2021, 11:45pm
19
You are my new best FRIEND !!!
For the record this is what your patients has produced for this noob and hopefully this can help others
$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
foreach($file in $files){
$old = get-content $file.FullName
$newfile = $old -replace "oldname","new_name"
$newfile | out-file $file.FullName -Encoding ascii -Force
$newname = $file.name -replace "old_name","new_name"
Rename-item $file.fullname -NewName $newname
}
Once again thank you this has been a life saver!!