Posted On October 2, 2020

PowerShell: Output HashTable to CSV

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Output HashTable to CSV
function outputHashtableToCsv{
    param(
        $hashTable,
        $csvFile='C:\updateResults.csv',
        $headers=@('computerName','minutesToUpdate')
        )
    try{
        write-host $($hashTable|out-string)
        if(test-path $csvFile){        
            rename-item $csvFile "$csvFile.bak"
            write-warning "$csvFile currently exists. Hence, that previous file has been renamed to $csvFile.bak"
        }
        $hashTable.GetEnumerator() | `
            Select-Object -Property @{N=$headers[0];E={$_.Key}}, @{N=$headers[1];E={$_.Value}} | `
            Export-Csv -NoTypeInformation -Path $csvFile
        write-host "CSV has been created successfully: $csvFile"
        return $true
    }catch{
        write-warning $_
        return $false
    }    
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to Install SSL Certificate(s) on Various Web Servers

Public facing websites often become become targets of attacks such as eavesdropping, denial of service,…

Python: Package Installer for Python (PIP)

This comes preinstalled with Python 3.4 or higher. Similar to the Microsoft PowerShell Gallery, Python.org…

Install Files within the NewPCSetupFiles Template Directory

net use y: \\FILESERVER01.kimconnect.com\users\template\newPCSetup y: regedit.exe /s disable_uac_win7.reg cd NewPCSetupFiles dotnetfx-winfsc.exe /Q dotNetFx40_Full_x86_x64.exe /quiet /norestartdotnetfx45_full_x86_x64.exe…