PowerShell - wmic - put only value in variable

Sebastian Jesior 166 Reputation points
2023-01-14T11:23:36.97+00:00

Hello,

May I kindly ask you to help me.

I would like to put in variable only "VMware Virtual Platform":

User's image

Is it possible to do this for wmic ?

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,537 questions
0 comments No comments
{count} votes

Accepted answer
  1. TP 101.3K Reputation points
    2023-01-14T11:41:33.2466667+00:00

    Hi,

    For this I recommend using the CIM cmdlets instead of wmic. Example below:

    $csProductName = (Get-CimInstance -ClassName Win32_ComputerSystemProduct).Name
    

    Here is older Get-WmiObject equivalent of above:

    $csProductName = (Get-WmiObject -Class Win32_ComputerSystemProduct).Name
    

    Introduction to CIM Cmdlets

    https://devblogs.microsoft.com/powershell/introduction-to-cim-cmdlets/

    Win32_ComputerSystemProduct class

    https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystemproduct

    If above is useful please Accept Answer.

    Thanks.

    -TP

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. MotoX80 34,951 Reputation points
    2023-01-14T13:22:33.4666667+00:00

    I need to somehow use wmic

    Why?

    Wmic.exe is an executable that issues WMI calls. It is being deprecated by Microsoft. The examples provided by @tl issue the same WMI calls and make it easier to parse the returned data. If you are writing a Powershell script, you should be using Get-CimInstance.

    With WMIC you need to parse it's stdout and search for the information.

    $data = wmic csproduct get name
    "Array count is {0}" -f $data.Count
    for ($i = 0; $i -lt $data.Count; $i++) {
        "Index = {0}, Data = {1}" -f $i, $data[$i]
    }
    "Here is index 2: {0}" -f $data[2]
    

    Update: Use -Computername on the Get-CimInstance call.

    2 people found this answer helpful.
    0 comments No comments

  2. Sebastian Jesior 166 Reputation points
    2023-01-14T12:43:54.76+00:00

    For example. We have different servers. We have also historical servers(win2003). Now I need command for all of them(win2003, win 2012, win2016 etc). For example:

    PS C:\Users\Administrator\Desktop> wmic /Node:X.X.X.X bios get serialnumber

    SerialNumber

    VMware-XX.XX.XX.XX.XX

    But I don't need 'SerialNumber' displayed because I would like to add ony this value VMware-XX.XX.XX.XX.XX to powershell variable.

    Some of these servers don't have Powershell installed (historical servers)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.