Contact Info

Crumbtrail » Administration » Powershell » Powershell 1.0 » Rename-Item

Rename-Item - Powershell 1.0 CmdLet

Microsoft Windows PowerShell is a command-line shell and scripting tool based on the Microsoft .NET Framework. It is designed for system administrators, engineers and developers to control and automate the administration of Windows and applications.

More than hundred command-line tools (so called "cmdlets") can be used to perform system administration tasks and Windows Management Instrumentation (WMI). These cmdlets are easy to use, with standard naming conventions and common parameters, and standard tools for piping, sorting, filtering, and formatting data and objects.


Change the name of an existing item


-path string
       The path(s) to the item(s) to be renamed. Wildcards are permitted.

   -newName string 
       The new name for the item.

       Override restrictions that prevent the command from succeeding, apart
       from security settings. e.g. Force will create file path directories 
       or override a files read-only attribute, but will not change file permissions.

        Pass the object created by Rename-Item along the pipeline. 
   -credential PSCredential
       Use a credential to validate access to the file. Credential represents
       a user-name, such as "User01" or "Domain01\User01", or a PSCredential
       object, such as the one retrieved by using the Get-Credential cmdlet.
       If you type a user name, you will be prompted for a password.
       This parameter is not supported by any PowerShell core cmdlets or providers.
       Describe what would happen if you executed the command without
       actually executing the command.

       Prompt for confirmation before executing the command.

      -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.

Rename a file:

PS C:\>rename-item -path c:\docs\dump.csv -newname Report.xls

Rename all .TXT files as .LOG files in the current directory:

PS C:\>get-childitem -Path *.txt | rename-item -NewName {$ -replace".txt",".log"}

Because the -Newname parameter does not accept wildcards, the above works by piping the output of Get-ChildItem and then using a -replace expression to calculate all the new filenames.