to rename a file we use Rename-Item command in powershell the command is very simple
Rename-Item $source $dest -force
But what about changing a folder/directory name? I made mistake to write the same command in case of folder/directory rename and it does not work, shows exception file not found which proves that the command is searching for a file not ready to rename and folder/directory.
After googling for some minutes I found the solution and the command is
Rename-Item -path $destFolder -newName $folderNewName
Example: Rename-Item -path ‘C:\AshrafTest\MyFolder’ -newName ‘MyFolder_New’
By the way the folder should contain some data to be rename. Pretty simple but wasted time to search internet so sharing 🙂
Hi,
thanx for this, but lets make it more complex: what if you need to rename just the first part of a foldername ?
I’m just a beginner in PS and need to do something for a certain dept. in our org.
example: you have different groups with same structure:
TEMPA
–>x-folderAA
–>x-folderAB
–>x-folderAC
TEMPB
–>x-folderBA
–>x-folderBB
TEMPC
–>x-folderCX
–>x-folderCY
and each ‘x-‘ from each folder needs to be renamed to ‘yy-‘ (so just in the beginning of the names).
thanx for posting a possible sollution.
Kind regards,
Radegonda
Can u Help me with this.. im getting an error……….
$now=Get-Date -Format “dd_MM_yyyy” | Out-String
Rename-Item -NewName $now -Path “C:\scripts\krushna”
=====================================================
PS C:\Users\kvrpakkiragari> $now=Get-Date -Format “dd_MM_yyyy” | Out-String
Rename-Item -NewName $now -Path “C:\scripts\krushna”
Rename-Item : Illegal characters in path.
At line:2 char:1
+ Rename-Item -NewName $now -Path “C:\scripts\krushna”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Rename-Item], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RenameItemCommand