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

WinRM Issues Caused by Service Principal Name (SPN) HTTP Protocol & Port Associations

Solution as Code: There is no easy solution to this problem. Certain IIS Pool Accounts…

PowerShell: Synchronize Files Between Different Domains

<# File_Copy_Script_UNC_to_Local_V0.1.ps1Purpose: connect to an external domain to copy files onto a Intranet server.Features:- Synchronize…

PowerShell: Function to Wait for Service to Be Back Online (After Server Reboots)

Function waitForService{ $testSucceeded=(Test-NetConnection $server -port $port).TcpTestSucceeded $null=Set-PSBreakpoint -Variable rightNow -Mode Read -Action { $global:testSucceeded =…
Other project: DragonCoin.com