How can I rename files in PowerShell? - powershell

I have 2 tasks that I'd like to use PowerShell for:
1 - I also need to rename all the index.asp to Default.aspx
2 - I have a directory C:\WWW where I need to rename all the .asp files to .aspx, recursively.
I have tried the Rename-Item command but always get errors.
Cannot create a file when that file already exists
How can I rename files in PowerShell?

Use the Get-ChildItem cmdlet to get all index.asp files. Pipe the result to the Rename-Item cmdlet and give the files the new name.
Get-ChildItem c:\www -Filter Index.asp -Recurse | Where-Object {$_.Extension -eq '.asp' } | Rename-Item -NewName Default.aspx
, that will get you index.aspx files as well so pipe the result to the `Where-Object' cmdlet and filter based on the file extension
Do the same for asp files, notice that now you will get .aspx files as well so pipe the result to the `Where-Object' cmdlet and filter based on the file extension. in the new name, take just the base name of each file (without extension) and append it '.aspx'
Get-ChildItem c:\www -Filter *.asp -Recurse | Where-Object {$_.Extension -eq '.asp' } | Rename-Item -NewName {$_.BaseName + '.aspx'}

Related

Renaming all the folder names in the current dir that has files and sub directories inside using Powershell

I have a set of folders like below, which has files and subfolders inside them, i want to rename
all the smv2103* folders to smv2106* folders , keeping the files and subfolders inside them intact using Powershell
E:\online\smv2103pdf
E:\online\smv2103mac
E:\online\smv2103frp
E:\online\smv2103rep
E:\online\smv2103soc
E:\online\smv2103bid
E:\online\smv2103rem
E:\online\smv2103nop
E:\online\smv2103gac
E:\online\smv2103pam
Any help would be highly appreciated , as Rename-Item itself is not working because of the files and subfolders inside
Get-ChildItem "E:\online\*" -Directory|
Where{ ( $_.Name -like "*2103*") } |
ForEach-Object{
((Get-Content $_.FullName -Raw) -replace "2103","2106") | Set-Content -Path $_.FullName
}
So you just want to rename the folders? You use Get-Content to get the content from within files. To filter for a certain criteria, Get-ChildItem has a -Filter Parameter you can use.
Get-ChildItem -Filter "*2013*"
When filtering in powershell, its good practice filtering as far left as possible. Now, knowing your just looking to rename the folders themselves, we can remove Get-Content and pipe the info onto Rename-Item.
Get-ChildItem -Path "E:\online*" -Filter "*2013*" -Directory |
ForEach-Object {
Rename-Item -Path $_.FullName -NewName $_.Name.Replace('2013','2016')
}

How to find all folders which has folder named "Intro" AND filetype *.mp4

The closest I got was using powershell Get-ChildItem given multiple -Filters
Get-ChildItem -Include "Intro", "*.mp4" -Recurse
I think, -Include with multiple params work as OR operator. It gives folders with either "Intro" folder OR "*.mp4" files.
But I need AND condition. Folder must contain a folder named "Intro" and "*.mp4" files.
I need folders structured following -
E:.
└───test1.mp4
└───test2.mp4
└───test3.mp4
└───test4.mp4
└───test5.mp4
└───test6.mp4
└───Intro
Update 1
I am searching for folders which meet two condition.
It must have a subfolder named Intro AND
It must have *.mp4 files.
The answer would look something like the following I guess.
Get-ChildItem -Directory -Recurse {HasSubFolderNamedIntro && HasMP4Files}
Just wanted to add another Method (one-liner) to get the folder which contains mp4 files and a folder Intro (probs to #HenrikStanleyMortensen for most of it):
(Get-ChildItem -include "*.mp4" -Recurse -File).DirectoryName | Select-Object -Unique | Where-Object {(Get-ChildItem $_ -Recurse -Include 'Intro' -Directory)}
Do you need the command to only return the file objects or do you also want the folder objects?
If just the files I would do it like this:
Get-ChildItem -include "*.mp4" -Recurse -File | Where-Object {$_.Directory.Name -match 'Intro'}
So we use the include to find the mp4 files and reduce the amount of objects we pipe.
Then we pipe it to Where-Object and look for the property with the name of the folder and says we want it to contain the word "intro". If the folder needs to be called Intro exactly and not just contain it you can change the -match to -eq
Edit
To get the directories then we could do it like this:
(Get-ChildItem -include "*.mp4" -Recurse | Where-Object {$_.Directory.Name -match 'Intro'}).DirectoryName | Select-Object -Unique
Now we say that all the files that we found that matches our search, we want to see the full directory path of those files.
To only get one match per directory, so if we have 1 directory that matches with multiple mp4 files, and we don't want to see that same directory in our output one time per file, we can pipe the result into Select-Object -Uniqueto only see each directory once.
Edit 2
After clarification from OP.
To find a folder that contains both mp4 files and a subfolder called intro I don't think we can do that only from the Get-ChildItem command in any way I know of, so we can loop through each folder like this:
$Files = (Get-ChildItem -include "*.mp4" -Recurse -File).DirectoryName | Select-Object -Unique
foreach($File in $Files) {
Get-ChildItem -Path $File.DirectoryName -Recurse -Include 'Intro' -Directory
}
We Pipe to the Select-Object -Unique to make sure that folders with multiple mp4 files are not looped through more than once thus giving us an output with the same intro folder multiple times.

Renaming multiple files in Windows

I have a folder containing multiple .txt files which have been created by another program. However the program outputs each file with "-Module" in the
filename. eg
filename-Module.txt
filename1-Module.txt
filename2-Module.txt
I would like to know if there's a script or command (Powershell/cmd) that I can run which will iterate over each file and remove the "-Module" from each filename so I simply end up with
filename.txt
filename1.txt
filename2.txt
I have tried using cmd from the directory, with the following:
rename *-Module.txt *txt
This resulted in no change.
You can use get-childitem and pipe it into rename-item.
get-childitem -path 'c:\folderwherefilesarelocated' -recurse |
rename-item -newname {$_.Name -replace "-module", ""}
Relatively straightforward:
Get-ChildItem ComputerName\TestLocation\testfolder -filter "*-module*" | Rename-Item -NewName {$_.name -replace '-module', ''}
Get the items in the folder, look for filenames with "-module" in them, and then replace the "-module" text with an empty string. You can also append a -whatif to see the output before performing this action.

Scan out a folder + subfolder + get the files with a specific extension in PowerShell

I have a folder named LOGFILES with a subfolder named LOGFILES_OLD.
But we need to assume that I don't know the contents of the folder.
What I'm trying to develop in PowerShell is a method to show a list of all files with a .log extension.
I have failed to get it with something I have found here:
Get-Item C:\LOGFILES -Recurse | Where-Object {$_.Extension -eq ".log"} | Format-List
Why don't you use :
Get-ChildItem "C:\LOGFILES" -include *.log -Recurse

Rename parts of file names that match a pattern

Say I have a list of file names like this:
some-file.ts
my-project-service.ts
other.ts
something-my-project.ts
I need to change the file names that have my-project in them to have just that part renamed to $appname$.
So my-project-service.ts would become $appname$-service.ts
And I need to do this recursively from a root directory.
I seem to be to be hopeless at PowerShell so I thought I would ask here to see if anyone can help me out.
Use the Get-ChildItem cmdlet with the -recurse switch to get all items recursivly from a root directory. Filter all items containing my-project using the Where-Object cmdlet and finally rename them using the Rename-Item cmdlet:
Get-ChildItem "D:\tmp" -Recurse |
Where {$_.Name -Match 'my-project'} |
Rename-Item -NewName {$_.name -replace 'my-project','$appname$' }