Snippet Name: Rename_Files_PowerShell
Tags:
Created Date: 2025-06-16 13:12:39
Last Modified Date: 2025-05-31 10:31:51
Dependencies:
DBA Notes:
Sample Output:
Title: Rename Files PowerShell
Author: user
i have a folder full of *.txt files i need to rename to have the extension *.sql instead. the folder is G:\My Drive\_Data\ChatGPT\ChatGPTHistory. how can i do that in PowerShell
Author: ChatGPT
You can use the following PowerShell script to rename all the `.txt` files in the specified folder to have the `.sql` extension:
```powershell
$folderPath = "G:\My Drive\_Data\ChatGPT\ChatGPTHistory"
Get-ChildItem -Path $folderPath -Filter "*.txt" | ForEach-Object {
$newName = $_.Name -replace "\.txt$", ".sql"
Rename-Item -Path $_.FullName -NewName $newName
}
```
This script will:
1. Set the `$folderPath` variable to your folder path.
2. Use `Get-ChildItem` to get all `.txt` files in the folder.
3. For each file, it replaces the `.txt` extension with `.sql` and renames the file using `Rename-Item`.
Make sure to run the script in a PowerShell session with appropriate permissions.