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 \":frowning:\" I’m a noob to scripting, only ever normal have to do login scripts and easy stuff.<\/p>\n

Advertisement

I need to maintain the date format on the files so they need to look like example new_businessname06092021.csv<\/p>\n

Get-ChildItem –Path \"C:\\temp\\AMLReports\" -Recurse -Filter *.csv\nforeach($file in Get-ChildItem $SomeFolder)\n{\n    $newname = ([String]$File).Replace(\"oldbuisnessname\",\"newbusinessname\")\n    Rename-item -Path \"C:\\temp\\AMLReports\\Renamed\" $File $newname\n}\n\n<\/code><\/pre>\n

Advertisement

Help appreciated \":slight_smile:\"<\/p>","upvoteCount":3,"answerCount":19,"datePublished":"2021-06-09T02:38:06.000Z","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"},"acceptedAnswer":{"@type":"Answer","text":"

how about like so?<\/p>\n

$files = Get-ChildItem \"C:\\temp\\AMLReports\" *.csv -Recurse\n\nforeach($file in $files){\n    $newname = $file.name -replace \"oldname\",\"newname\"\n    Rename-item $file.fullname -NewName $newname -verbose\n}\n<\/code><\/pre>\n

Test it before<\/strong> you let it lose in your prod environment.<\/p>","upvoteCount":0,"datePublished":"2021-06-09T02:57:02.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"

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 \":frowning:\" I’m a noob to scripting, only ever normal have to do login scripts and easy stuff.<\/p>\n

I need to maintain the date format on the files so they need to look like example new_businessname06092021.csv<\/p>\n

Get-ChildItem –Path \"C:\\temp\\AMLReports\" -Recurse -Filter *.csv\nforeach($file in Get-ChildItem $SomeFolder)\n{\n    $newname = ([String]$File).Replace(\"oldbuisnessname\",\"newbusinessname\")\n    Rename-item -Path \"C:\\temp\\AMLReports\\Renamed\" $File $newname\n}\n\n<\/code><\/pre>\n

Help appreciated \":slight_smile:\"<\/p>","upvoteCount":3,"datePublished":"2021-06-09T02:38:07.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/1","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

Welcome<\/p>\n

If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n

\n
\n
<\/div>\n \"\"\n PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> Programming & Development<\/span><\/span><\/a>\n <\/div>\n
\n Hi, and welcome to the PowerShell forum! \n\n\nDon’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!\nUse 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.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n

\"codebutton_small.png\"<\/p>","upvoteCount":0,"datePublished":"2021-06-09T02:52:41.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

How did you make that so easy!<\/p>\n

I wish I had your brain \":slight_smile:\"<\/p>\n

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<\/p>\n

powershell -Command \"(gc INPUT.csv) -replace 'oldname', 'newname' | Out-File -encoding ASCII OUTPUT.csv\"\n<\/code><\/pre>\n

I have know idea how to incorporate this into the script you fixed up for me, thanks heaps!<\/p>\n

this community is amazing<\/p>\n

Miles<\/p>","upvoteCount":0,"datePublished":"2021-06-09T03:04:18.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/4","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

I’d update the content first and then rename.<\/p>\n

$files = Get-ChildItem Get-ChildItem \"C:\\temp\\AMLReports\" *.csv -Recurse\n\nforeach($file in $files){\n    $old     = get-content $file.FullName\n    $newfile =  $old -replace \"oldname\",\"newname\"\n    $newfile | out-file $file.FullName -Force\n\n    $newname = $file.name -replace \"oldname\",\"newname\"\n    Rename-item $file.fullname -NewName $newname\n}\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-09T03:09:35.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

Morning from NZ<\/p>\n

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)<\/p>\n

Just for clarification<\/p>\n

I have a folder that contains hundreds of folders that have csv files in them that have been named oldbusinessname-date.csv<\/p>\n

These files all need renamed to newbusinessname-date.csv (This Script tested working for this) Thanks!<\/p>\n

$files = Get-ChildItem \"C:\\temp\\AMLReports\" *.csv -Recurse\n\nforeach($file in $files){\n    $newname = $file.name -replace \"oldname\",\"newname\"\n    Rename-item $file.fullname -NewName $newname -verbose\n}\n<\/code><\/pre>\n

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.<\/p>\n

I tried running this and PS just opened and closed without running any lines and no error.<\/p>\n

$files = Get-ChildItem Get-ChildItem \"C:\\temp\\AMLReports\" *.csv -Recurse\n\nforeach($file in $files){\n    $old     = get-content $file.FullName\n    $newfile =  $old -replace \"oldname\",\"newname\"\n    $newfile | out-file $file.FullName -Force\n\n    $newname = $file.name -replace \"oldname\",\"newname\"\n    Rename-item $file.fullname -NewName $newname\n}\n<\/code><\/pre>\n

I do very much appreciate you taking the time to help me!<\/p>\n

Miles<\/p>","upvoteCount":0,"datePublished":"2021-06-09T19:16:47.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/6","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" MILESit:<\/div>\n
\n

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<\/p>\n<\/blockquote>\n<\/aside>\n

if you run it just like this, powershell would not indicate if anything was done, you’d just see red if it fails.<\/p>\n

Did it actually update stuff? It worked fine in my test<\/p>\n

I do not understand what you mean by \" closed without running any lines\" ? What do expect?<\/p>\n

to see if powershell is actually doing something you need to add ‘-verbose’<\/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\",\"newname\"\n    $newfile | out-file $file.FullName -Force\n    write-verbose \"Updated $($file.fullname)\" -verbose\n\n    $newname = $file.name -replace \"oldname\",\"newname\"\n    Rename-item $file.fullname -NewName $newname -Verbose\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-09T19:21:55.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

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?<\/p>","upvoteCount":0,"datePublished":"2021-06-09T19:38:53.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/8","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

Here is a csv sample if it helps<\/p>\n

297a4f68-59f3-4308-bb9b-4cda3d68b0a9-oldname_20210607_138_Customers_43.csv<\/a> (855 Bytes)<\/p>","upvoteCount":0,"datePublished":"2021-06-09T19:44:24.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/9","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

the CSV should not matter.<\/p>\n

Yes, as admin would help.<\/p>\n

how do you run it?<\/p>\n

$files = Get-ChildItem \"C:\\temp\\AMLReports\" *.csv -Recurse\n$files.count\n<\/code><\/pre>\n

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?<\/p>","upvoteCount":0,"datePublished":"2021-06-09T19:47:09.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/10","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

The same thing happens with that, just opens and closes.<\/p>\n

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<\/p>\n

I’m wanting to run script as a daily scheduled task.<\/p>\n

I’m not sure how to run as the file as admin, no shift right click options on the file<\/p>\n

I get the attached with run from within the ps console<\/p>\n

\"44f0d92b-78ad-427f-86dd-f6286c69cdee-Capture.PNG\"
<\/use><\/svg>44f0d92b-78ad-427f-86dd-f6286c69cdee-Capture.PNG<\/span>1274×637 30.2 KB<\/span><\/use><\/svg><\/div><\/a><\/div><\/p>","upvoteCount":0,"datePublished":"2021-06-09T22:51:56.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/11","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

you open the powershell console as admin and then just call the script.<\/p>\n

looks like you have also execution policy restriction.<\/p>\n

\n
\n \n\n ATA Learning – 16 Mar 21<\/a>\n <\/header>\n\n
\n
<\/div>\n\n

Set-ExecutionPolicy for Managing PowerShell Execution Policies<\/a><\/h3>\n\n

Learn all about PowerShell execution policies, various scopes, contexts, how to use Get-ExecutionPolicy and Set-ExecutionPolicy.<\/p>\n\n\n <\/article>\n\n

\n \n \n <\/div>\n\n
<\/div>\n<\/aside>\n\n

in task scheduler you’d call it like so<\/p>\n

powershell.exe -executionpolicy bypass -file \"c:\\scripts\\script.ps1\"\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-09T22:55:44.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/12","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

It works if I run PS as admin I get the file count, ill test now running the rename files \":slight_smile:\"<\/p>\n

ITS working! running as admin helped \":slight_smile:\" 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?<\/p>","upvoteCount":0,"datePublished":"2021-06-09T22:57:20.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/13","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" MILESit:<\/div>\n
\n

It works if I run PS as admin I get the file count, ill test now running the rename files \":slight_smile:\"<\/p>\n

ITS working! running as admin helped \":slight_smile:\" 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?<\/p>\n<\/blockquote>\n<\/aside>\n

the encoding and such depends on your CSV files.<\/p>\n

out-file has a few build in encoding option:<\/p>\n

\"0f6e40c0-2bca-496d-ba9f-e88414cad1fb-2021-06-09_18-02-26.png\"
<\/use><\/svg>0f6e40c0-2bca-496d-ba9f-e88414cad1fb-2021-06-09_18-02-26.png<\/span>725×76 3.06 KB<\/span><\/use><\/svg><\/div><\/a><\/div><\/p>\n

in my test it seemed to work fine<\/p>\n

$csv = get-content \".\\Input.csv\"\n$new = $csv -replace \"dumbo\",\"neally\"\n$new | out-file  \".\\Input2.csv\"\n<\/code><\/pre>\n

\"8477024d-8960-4b2a-83d6-5a094a57cfc6-2021-06-09_18-06-45.png\"<\/p>","upvoteCount":0,"datePublished":"2021-06-09T23:07:04.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/14","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

yeah so the default encoding
\n → windows powershell (5.1) is unicode with BOM. (utf-16)
\n → powershell (7.x) is utf8NoBOM<\/p>\n

\n
\n\n learn.microsoft.com<\/a>\n <\/header>\n\n
\n \n\n

Out-File (Microsoft.PowerShell.Utility) - PowerShell<\/a><\/h3>\n\n

The Out-File cmdlet sends output to a file. It implicitly uses PowerShell's formatting system to write to the file. The file receives the same display representation as the terminal. This means that the output may not be ideal for programmatic...<\/p>\n\n\n <\/article>\n\n

\n \n \n <\/div>\n\n
<\/div>\n<\/aside>\n\n

so try like so<\/p>\n

out-file  $file.FullName -Encoding ascii -Force\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-09T23:11:40.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/15","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

On the working test I have just noticed that It did put this at the top not sure if that’s related.<\/p>\n

Get-ChildItem : A positional parameter cannot be found that accepts argument '*.csv'.\nAt line:1 char:10\n+ $files = Get-ChildItem Get-ChildItem \"C:\\temp\\AMLReports\" *.csv -Recu ...\n+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException\n    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand\n<\/code><\/pre>\n

I have attached the before file and after file for reference<\/p>\n

dd8dac5e-8056-447e-91a7-087134f5c811-OLDNAME_20210127_008_Customers_02-_PRIOR.csv<\/a> (846 Bytes)<\/p>","upvoteCount":0,"datePublished":"2021-06-09T23:28:19.000Z","url":"https://community.spiceworks.com/t/powershell-file-rename-recuse-help-needed/802189/16","author":{"@type":"Person","name":"milesit","url":"https://community.spiceworks.com/u/milesit"}},{"@type":"Answer","text":"

output<\/p>\n

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>\n

Once 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"}}]}}