Import txt file and export to csv file using powershell

AiFa 1 Reputation point

I want to retrieve computer information of list of servers from txt file format and export it to csv file using powershell but some objects were unidentified. Kindly help.

Comp hostname
OS info (eg, Wins 12/16/19)
Last Reboot Details
Number of CPU Cores
Memory/RAM (in GB)
C Disk Capacity and Usage %

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,354 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 95,411 Reputation points MVP

    HI @AiFa ,

    maybe these links help to get started:

    If you have started with a script please post it here using the Code Sample button (the icon with 101010).


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 44,621 Reputation points

    Here' one way of doing that:

    Get-Contents <Your-File-Name-Here> |
            $x = @{
                "Comp hostname" = ""
                "OS info" = ""
                "Uptime" = ""
                "Last Reboot Details" = ""
                "Manufacturer" = ""
                "Model" = ""
                "Number of CPU Cores" = ""
                "Memory/RAM (in GB)" = ""
                "C Disk Capacity and Usage %" = ""
            Switch -regex ($_){
                "^Comp hostname" {$s -match "^(Comp hostname)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^OS info"       {$s -match "^(OS info)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^Uptime"        {$s -match "^(Uptime)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^Last Reboot Details" {$s -match "^(Uptime)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^Manufacturer"  {$s -match "^(Manufacturer)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^Model"         {$s -match "^(Model)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^Number of CPU Cores" {$s -match "^(Number of CPU Cores)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^Memory/RAM \(in GB\)" {$s -match "^(Memory/RAM \(in GB\))\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; break}
                "^C Disk Capacity and Usage %" {$s -match "^(C Disk Capacity and Usage %)\s+(.*)$"|Out-Null; $x[$matches[1]]=$matches[2]; 
        } | Export-Csv <CSV-file-name-here> -NoTypeInformation
    0 comments No comments