Powershell basics

Batch operations for file objects.

Rename multiple files:

 Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' } 

The Get-ChildItem cmdlet gets all the files in the current folder that have a .txt file extension then pipes them to Rename-Item. The value of NewName is a script block that runs before the value is submitted to the NewName parameter.

In the script block, the $_ automatic variable represents each file object as it comes to the command through the pipeline. The script block uses the -replace operator to replace the file extension of each file with .log. Notice that matching using the -replace operator is not case sensitive.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.