PowerShell won’t rename an export .csv file

powershellrename

I am trying to rename the file export.csv to the $name but with a .csv extension instead of .file. The output is still $name.file.

Import-Module ActiveDirectory
$name = Read-Host "Enter Group Name"
Get-ADGroupMember -identity "$name" -Recursive |select name, SamAccountName| export-csv -path C:\export.csv -NoTypeInformation

get-childItem *.file | Rename-Item -newname {$name.name -replace '\.file', '.csv'}

So I did this and it works:

Import-Module ActiveDirectory
$name = Read-Host "Enter Group Name"
Get-ADGroupMember -identity "$name" -Recursive |select name, SamAccountName| export-csv -path h:\groups\export.csv -NoTypeInformation
rename-item "h:\groups\export.csv" $name
Dir h:\groups\ | Rename-Item -newname {$_.name + '.csv'}

But how do I get it to rename the file as I run the command instead of doing it for the whole directory every time? What keeps adding .csv?

Best Answer

The solution is to change this line: export-csv -path h:\groups\export.csv -NoTypeInformation to this: export-csv -path "h:\groups\$Name.csv" -NoTypeInformation

Related Topic