Alec
Alec

Reputation: 3

How can I optimize the command Get-ComputerInfo?

Every time I get a snippet of info from this command, Windows will run the whole function, which takes time. The script works, but its slow. Because of the way the rest of this code is written, I cannot store Get-ComputerInfo as a string because I still need to pull strings from it without the tediousness of using substrings. What are my options here?

A sample of my code is here:

# Get username and format
$USRNAME = Get-ComputerInfo -Property WindowsRegisteredOwner | Out-String
$USRNAME = $USRNAME.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$USRNAME = $USRNAME[2]


# Get BIOS S/N and format
$BIOSSN = Get-ComputerInfo -Property BiosSeralNumber | Out-String
$BIOSSN = $BIOSSN.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$BIOSSN = $BIOSSN[2]


# Get BIOS Manufacturer and format
$MANUFAC = Get-ComputerInfo -Property CsManufacturer | Out-String
$MANUFAC = $MANUFAC.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$MANUFAC = $MANUFAC[2]


# Get BIOS Model and format
$MODEL = Get-ComputerInfo -Property CsModel | Out-String
$MODEL = $MODEL.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
$MODEL = $MODEL[2..25]

As mentioned previously, I tried to output as a string and it got messy.

Upvotes: 0

Views: 407

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Call Get-ComputerInfo once, the store the output object in a variable, and then finally reuse that:

I still need to pull strings from it without the tediousness of using substrings

You really don't - Get-ComputerInfo outputs an object that has each piece of information neatly stored in separate properties - to get the WindowsRegisteredOwner value, simply use the . member access operator to dereference the WindowsRegisteredOwner property, and so on for the remaining properties:

$allInfo = Get-ComputerInfo -Property WindowsRegisteredOwner, BiosSeralNumber, CsManufacturer, CsModel

$USRNAME = $allInfo.WindowsRegisteredOwner
$BIOSSN  = $allInfo.BiosSeralNumber
$MANUFAC = $allInfo.CsManufacturer
$MODEL   = $allInfo.CsModel

Upvotes: 3

Related Questions