Import txt file and export to csv file using powershell

AiFa 1 Reputation point
2022-01-28T16:22:46.137+00:00

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)
Uptime
Last Reboot Details
Manufacturer
Model
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
    2022-01-29T00:24:43.153+00:00

    HI @AiFa ,

    maybe these links help to get started:
    https://www.thomasmaurer.ch/2021/03/get-system-uptime-with-powershell/
    https://learn.microsoft.com/en-us/powershell/scripting/samples/collecting-information-about-computers?view=powershell-7.2
    https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-get-computer-name/

    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)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 44,621 Reputation points
    2022-01-29T03:42:32.557+00:00

    Here' one way of doing that:

    Get-Contents <Your-File-Name-Here> |
        ForEach-Object{
            $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]; 
                                                    [PSCustomObject]$x                  
                                                    break}
            }
        } | Export-Csv <CSV-file-name-here> -NoTypeInformation
    
    0 comments No comments