Forum Discussion
Unable to save the Output to file location
Hi,
Please find below code. I am unable to save the Output to file location. I have created the $Filepath to create new folder with year, if it is created it will ignore automatically since I have used to -force command.
$Filepath = New-Item -ItemType Directory -Force -Path "D:\ADComputers\$((Get-Date).tostring("yyyy"))"
Get-ADComputer -Filter * -Property * | Select-Object Name,enabled,OperatingSystem,OperatingSystemVersion,ipv4Address,lastlogondate,CanonicalName |
Export-CSV -Path ($Filepath + '\' + 'ADComputers_Report_$((Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")).csv' ) -Force -Delimiter ',' -NoTypeInformation -Encoding UTF8
Appreciate for your help.
Regards
Govindu Sreeharsha
You're putting the command to create the folder in a variable, that's one reason. Other reason is the formatting of the file, I changed it a little bit 🙂
New-Item -ItemType Directory -Force -Path "C:\ADComputers\$((Get-Date).tostring("yyyy"))" $Filepath = "C:\ADComputers\$((Get-Date).tostring("yyyy"))" $filename=(Get-Date).tostring("dd-MM-yyyy-hh-mm-ss") Get-ADComputer -Filter * -Property * | Select-Object Name,enabled,OperatingSystem,OperatingSystemVersion,ipv4Address,lastlogondate,CanonicalName | Export-CSV -Path ($Filepath + '\'+ "ÁDComputers_Report_$($filename).csv") -Force -Delimiter ','-NoTypeInformation -Encoding UTF8
4 Replies
- Jonathan_AllenBrass Contributor
Just for some variety here is the way that I would write the code that you have shared (with the exception of adding any error handling)
$Path = "D:\ADComputers\{0:yyyy}" -f (Get-Date) $Filepath = New-Item -ItemType Directory -Force -Path $Path Get-ADComputer -Filter * -Property * | Select-Object Name,enabled,OperatingSystem,OperatingSystemVersion,ipv4Address,lastlogondate,CanonicalName | Export-CSV -Path (join-path $Filepath ('ADComputers_Report_{0:"dd-MM-yyyy-hh-mm-ss"}.csv' -f (Get-Date) ) ) -Force -Delimiter ',' -NoTypeInformation -Encoding UTF8
This uses the -f formatting operator to take inputs and insert them into a string with desired formatting and allows for the constructed string to remain readable and not confused with excessive parenthesis etc.
I am also using the Join-Path command to provide the path for Export-Csv so that we are confident that it is a well formed path and we dont need to concatenate strings.
Over time I have found that this approach makes code maintenance simpler and quicker.
You're putting the command to create the folder in a variable, that's one reason. Other reason is the formatting of the file, I changed it a little bit 🙂
New-Item -ItemType Directory -Force -Path "C:\ADComputers\$((Get-Date).tostring("yyyy"))" $Filepath = "C:\ADComputers\$((Get-Date).tostring("yyyy"))" $filename=(Get-Date).tostring("dd-MM-yyyy-hh-mm-ss") Get-ADComputer -Filter * -Property * | Select-Object Name,enabled,OperatingSystem,OperatingSystemVersion,ipv4Address,lastlogondate,CanonicalName | Export-CSV -Path ($Filepath + '\'+ "ÁDComputers_Report_$($filename).csv") -Force -Delimiter ','-NoTypeInformation -Encoding UTF8
- Bittu_8148Copper Contributor
Harm_Veenstra
Thanks for the assistance. It's perfect what I have imagined.- No problem, please mark the answer as solution 😊