Search code examples
powershellcsvscriptingenvironment-variablesexport-to-csv

Using Powershell environmental variables as strings when outputting files


I am using Get-WindowsAutopilotInfo to generate a computer's serial number and a hash code and export that info as a CSV.

Here is the code I usually use:

new-item "c:\Autopilot_Export" -type directory -force

Set-Location "C:\Autopilot_Export"

Get-WindowsAutopilotInfo.ps1 -OutputFile Autopilot_CSV.csv

Robocopy C:\Autopilot_Export \\Zapp\pc\Hash_Exports /copyall

This outputs a CSV file named "Autopilot_CSV.csv" to the C:\Autopilot_Export directory and then Robocopy copies it to the Network Share drive inside of the Hash_Exports folder listed above. In the above code, if I manually type in "test", "123", "ABC", etc. and replace "Autopilot_CSV" it will output a CSV under all of those names as well. So it looks like Get-WindowsAutopilotInfo will create a CSV file and save the file name with whatever string you pass into it. Great.

However, I am running this script on multiple different computers and I would like the exported CSV to be named something unique to the machine it is running on so I can easily identify it once it's copied. I am trying to pass the value of the environmental variable $env:computername as a string input for the CSV and it isn't working.

Here's the code I'm trying to use:

new-item "c:\Autopilot_Export" -type directory -force

Set-Location "C:\Autopilot_Export"

Get-WindowsAutopilotInfo.ps1 -OutputFile $env:computername.csv

Robocopy C:\Autopilot_Export C:\Users\danieln\Desktop /copyall

This code does not generate the CSV file at all. Why not?

Do I just have a basic misunderstanding of how environmental variables are used in Powershell? $env:computername appears to return a string of the computer's name, but I cannot pass it into Get-WindowsAutopilotInfo and have it save, but it will work if I manually type a string in as the input.

I have also tried setting it to a variable $computername = [string]$env:computername and just passing $computername in before the .CSV and that doesn't appear to work either. And per the docmentation, environmental variables are apparently already strings.

Am I missing something?

Thanks!


Solution

  • Doublequoting seems to work. The colon is special in powershell parameters.

    echo hi | set-content "$env:computername.csv"
    
    dir
    
    
        Directory: C:\users\me\foo
    
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a----         2/11/2021   1:02 PM              4 COMP001.csv
    

    The colon is special. For example in switch parameters:

    get-childitem -force:$true
    

    Actually, it's trying to find a property named csv:

    echo hi | Set-Content  $env:COMPUTERNAME.length
    dir
    
    
        Directory: C:\Users\me\foo
    
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a----         2/11/2021   3:04 PM              4 8