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

PowerShell: Set Virtual Machine Default Paths on Hyper-V Host of a Cluster

$newVirtualMachinePath='D:\VirtualMachines' $newVirtualHardDiskPath='D:\VirtualMachines' function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…

Basic JavaScript: Understanding Uninitialized Variables

// Initialize these three variablesvar a;var b;var c;// Do not change code below this linea…

PowerShell: Hyper-V Guest-VM C-Volume Expansion

# Hyper-V-Guest-VM-Volume-Expansion.ps1 # Set variables: $vmName="TESTWindows.intranet.kimconnect.net"; $driveLetter="C"; $newSize="160GB"; function selectDisk($vmname){ $paths=(get-vm $vmName | select -expand…