Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Do you get any errors? –  EliadTech Jun 26 '14 at 20:08
    
No it runs and exports the file and adds the .csv to the end but it dows it for the whole folder so when i start doing multiple groups i get file names like group3-name.csv, group2-name.csv.csv, group1name-.csv.csv.csv –  sur4 Jun 26 '14 at 20:48
    
Do you mean (export-csv -path "h:\groups\$Name.csv" -NoTypeInformation) ? –  EliadTech Jun 26 '14 at 20:54
    
Import-Module ActiveDirectory $name = Read-Host "Enter Group Name" Get-ADGroupMember -identity "$name" -Recursive |select name, SamAccountName| export-csv -path h:\groups\$Name.csv -NoTypeInformation this worked –  sur4 Jun 26 '14 at 21:02
    
Thank you I thought i had tried that, but I guess not. –  sur4 Jun 26 '14 at 21:03

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.