Powershell or VBS Alternatives for WMIC

Discussion in 'Scripting' started by adric, Feb 5, 2022.

  1. adric

    adric MDL Expert

    Jul 30, 2009
    1,251
    1,324
    60
    #1 adric, Feb 5, 2022
    Last edited: Feb 5, 2022
    Eventually if not already, people will be looking for solutions that no longer rely on WMIC commands on systems where WMIC is no longer supported. Sometimes a simple command line snippet in powershell or vbs is enough as a replacement for a wmic command. I'd be interested in seeing what others have found along the way to eventually replace wmic commands. Here is some information that I've gathered to start off this thread:

    WMIC Documetation
    "The WMI command-line (WMIC) utility is deprecated as of Windows 10, version 21H1, and as of the 21H1 semi-annual channel release of Windows Server. This utility is superseded by Windows PowerShell for WMI. This deprecation applies only to the WMI command-line (WMIC) utility; Windows Management Instrumentation (WMI) itself is not affected."

    https://stackoverflow.com/questions/57121875/what-can-i-do-about-wmic-is-deprecated
    "Commands in wmic are usually derived from WMI class names, but it's not really rule of thumb. With PowerShell you are accessing WMI by it's real class name instead, so you may need to seek for other classes if needed."

    WMIC aliases to real WMI classes can be obtained via wmic.exe alias list brief

    WMIC is deprecated.

    [global switches] <command>

    The following global switches are available:

    /NAMESPACE Path for the namespace the alias operate against.
    /ROLE Path for the role containing the alias definitions.
    /NODE Servers the alias will operate against.
    /IMPLEVEL Client impersonation level.
    /AUTHLEVEL Client authentication level.
    /LOCALE Language id the client should use.
    /PRIVILEGES Enable or disable all privileges.
    /TRACE Outputs debugging information to stderr.
    /RECORD Logs all input commands and output.
    /INTERACTIVE Sets or resets the interactive mode.
    /FAILFAST Sets or resets the FailFast mode.
    /USER User to be used during the session.
    /PASSWORD Password to be used for session login.
    /OUTPUT Specifies the mode for output redirection.
    /APPEND Specifies the mode for output redirection.
    /AGGREGATE Sets or resets aggregate mode.
    /AUTHORITY Specifies the <authority type> for the connection.
    /?[:<BRIEF|FULL>] Usage information.

    For more information on a specific global switch, type: switch-name /?

    The following alias/es are available in the current role:

    ALIAS - Access to the aliases available on the local system
    BASEBOARD - Base board (also known as a motherboard or system board) management.
    BIOS - Basic input/output services (BIOS) management.
    BOOTCONFIG - Boot configuration management.
    CDROM - CD-ROM management.
    COMPUTERSYSTEM - Computer system management.
    CPU - CPU management.
    CSPRODUCT - Computer system product information from SMBIOS.
    DATAFILE - DataFile Management.
    DCOMAPP - DCOM Application management.
    DESKTOP - User's Desktop management.
    DESKTOPMONITOR - Desktop Monitor management.
    DEVICEMEMORYADDRESS - Device memory addresses management.
    DISKDRIVE - Physical disk drive management.
    DISKQUOTA - Disk space usage for NTFS volumes.
    DMACHANNEL - Direct memory access (DMA) channel management.
    ENVIRONMENT - System environment settings management.
    FSDIR - Filesystem directory entry management.
    GROUP - Group account management.
    IDECONTROLLER - IDE Controller management.
    IRQ - Interrupt request line (IRQ) management.
    JOB - Provides access to the jobs scheduled using the schedule service.
    LOADORDER - Management of system services that define execution dependencies.
    LOGICALDISK - Local storage device management.
    LOGON - LOGON Sessions.
    MEMCACHE - Cache memory management.
    MEMORYCHIP - Memory chip information.
    MEMPHYSICAL - Computer system's physical memory management.
    NETCLIENT - Network Client management.
    NETLOGIN - Network login information (of a particular user) management.
    NETPROTOCOL - Protocols (and their network characteristics) management.
    NETUSE - Active network connection management.
    NIC - Network Interface Controller (NIC) management.
    NICCONFIG - Network adapter management.
    NTDOMAIN - NT Domain management.
    NTEVENT - Entries in the NT Event Log.
    NTEVENTLOG - NT eventlog file management.
    ONBOARDDEVICE - Management of common adapter devices built into the motherboard (system board).
    OS - Installed Operating System/s management.
    PAGEFILE - Virtual memory file swapping management.
    PAGEFILESET - Page file settings management.
    PARTITION - Management of partitioned areas of a physical disk.
    PORT - I/O port management.
    PORTCONNECTOR - Physical connection ports management.
    PRINTER - Printer device management.
    PRINTERCONFIG - Printer device configuration management.
    PRINTJOB - Print job management.
    PROCESS - Process management.
    PRODUCT - Installation package task management.
    QFE - Quick Fix Engineering.
    QUOTASETTING - Setting information for disk quotas on a volume.
    RDACCOUNT - Remote Desktop connection permission management.
    RDNIC - Remote Desktop connection management on a specific network adapter.
    RDPERMISSIONS - Permissions to a specific Remote Desktop connection.
    RDTOGGLE - Turning Remote Desktop listener on or off remotely.
    RECOVEROS - Information that will be gathered from memory when the operating system fails.
    REGISTRY - Computer system registry management.
    SCSICONTROLLER - SCSI Controller management.
    SERVER - Server information management.
    SERVICE - Service application management.
    SHADOWCOPY - Shadow copy management.
    SHADOWSTORAGE - Shadow copy storage area management.
    SHARE - Shared resource management.
    SOFTWAREELEMENT - Management of the elements of a software product installed on a system.
    SOFTWAREFEATURE - Management of software product subsets of SoftwareElement.
    SOUNDDEV - Sound Device management.
    STARTUP - Management of commands that run automatically when users log onto the computer system.
    SYSACCOUNT - System account management.
    SYSDRIVER - Management of the system driver for a base service.
    SYSTEMENCLOSURE - Physical system enclosure management.
    SYSTEMSLOT - Management of physical connection points including ports, slots and peripherals, and proprietary connections points.
    TAPEDRIVE - Tape drive management.
    TEMPERATURE - Data management of a temperature sensor (electronic thermometer).
    TIMEZONE - Time zone data management.
    UPS - Uninterruptible power supply (UPS) management.
    USERACCOUNT - User account management.
    VOLTAGE - Voltage sensor (electronic voltmeter) data management.
    VOLUME - Local storage volume management.
    VOLUMEQUOTASETTING - Associates the disk quota setting with a specific disk volume.
    VOLUMEUSERQUOTA - Per user storage volume quota management.
    WMISET - WMI service operational parameters management.

    For more information on a specific alias, type: alias /?

    CLASS - Escapes to full WMI schema.
    PATH - Escapes to full WMI object paths.
    CONTEXT - Displays the state of all the global switches.
    QUIT/EXIT - Exits the program.

    For more information on CLASS/PATH/CONTEXT, type: (CLASS | PATH | CONTEXT) /?
    [Some WMIC to Powershell snippets]
    Code:
    wmic os get localdatetime /value
    powershell -nop -c "([WMI]'Win32_OperatingSystem=@').LocalDateTime"
    
    wmic os get BuildNumber
    powershell -nop -c "([WMI]'Win32_OperatingSystem=@').BuildNumber"
    
    for /f "tokens=1 delims==" %%a in ('powershell -nop -c "([WMI]'Win32_OperatingSystem=@').BuildNumber"') do (
      set /a WinBuild=%%a
    )
    :: or
    for /f "tokens=6 delims=[]. " %%a in ('ver') do set WinBuild=%%a
    
    wmic os get osarchitecture
    powershell "(Get-CimInStance CIM_OperatingSystem).OSArchitecture"
    
    wmic memorychip get capacity
    powershell Get-CimInstance -ClassName Win32_PhysicalMemory ^| Select-Object capacity
    
    wmic diskdrive get Status,Model
    powreshell Get-CimInstance -ClassName Win32_diskdrive ^| Select-Object status, model
    powershell Get-CimInstance -ClassName Win32_diskdrive ^| select status, model ^| ConvertTo-JSON
    
     
  2. Atari800XL

    Atari800XL MDL Addicted

    Apr 3, 2011
    956
    1,675
    30
    Excellent idea, thank you!

    In my PostInstall thingy, I use this WMIC code, I'm looking for a PS alternative:

    Code:
    WMIC ComputerSystem where Name="COMPUTER-NAME" call Rename Name=NewName
     
  3. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,745
    5,209
    120
    trying this. not work. interesting.
    Code:
    $test = Get-WmiObject -ClassName Win32_ComputerSystem
    $test.Rename("DESKTOP-16111223")
    $test.Put()
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    Powershell (Admin) and Reboot, then it works (just like doing it from system properties).
    (gwmi win32_computersystem).rename('MDL')
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. crypticus

    crypticus MDL Senior Member

    Jun 29, 2015
    317
    142
    10
    edit this for your custom name
    powershell Rename-Computer -NewName "Administrator"
     
  6. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,745
    5,209
    120
    #6 Dark Dinosaur, Feb 11, 2022
    Last edited: Feb 11, 2022
    Lets add another helpful Information.
    WMIC vs PowerShell vs VBS

    Code:
    set "QueryClass=SoftwareLicensingProduct"
    set "QueryTable=PartialProductKey, ID, Name"
    set "QueryFilter=name like '%%!pFilter!%%' And name like '%%KMS%%'"
    
    Wmic Version
    Code:
    wmic path %QueryClass% where (%QueryFilter%) get %QueryTable%
    
    PowerShell Version
    Code:
    >"%temp%\script.ps1" echo Get-WmiObject -Query "select * from %QueryClass% where %QueryFilter%" ^| format-table -Property %QueryTable%
    powershell -noprofile -executionpolicy bypass -file "%temp%\script.ps1"
    
    VBS Version
    Code:
     >"%temp%\script.vbs" echo on error resume next
    >>"%temp%\script.vbs" echo strQuery = "Select " ^+ "%QueryTable%" + " from " ^+ "%QueryClass%" ^+ " where " ^+ "%QueryFilter%"
    >>"%temp%\script.vbs" echo Set objArray= GetObject("winmgmts:\\.\root\CIMV2").ExecQuery(strQuery,,48)
    >>"%temp%\script.vbs" echo For each obj in objArray
    >>"%temp%\script.vbs" echo     result = ","
    >>"%temp%\script.vbs" echo     For each Prop in obj.Properties_
    >>"%temp%\script.vbs" echo         if NOT result = "," Then
    >>"%temp%\script.vbs" echo             result = result ^& Prop.Value ^& ","
    >>"%temp%\script.vbs" echo         End if
    >>"%temp%\script.vbs" echo         if result = "," Then
    >>"%temp%\script.vbs" echo             result = Prop.Value ^& ","
    >>"%temp%\script.vbs" echo         End if
    >>"%temp%\script.vbs" echo     Next
    >>"%temp%\script.vbs" echo     if NOT result = "," Then
    >>"%temp%\script.vbs" echo         WScript.Echo "%QueryTable%"
    >>"%temp%\script.vbs" echo         WScript.Echo result
    >>"%temp%\script.vbs" echo end if
    >>"%temp%\script.vbs" echo Next
    cscript //Nologo "%temp%\script.vbs"
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. m.fessler

    m.fessler MDL Novice

    Jan 15, 2020
    32
    10
    0
    Great idea - I looked through my batch files, here are some commands I used, including the "trappings" for better understanding.
    I'm grateful for every tip. Thank you!

    Code:
    for /f "tokens=1" %%i in ('wmic logicaldisk get volumename^, name ^| findstr /rc:"\<TES\>"') do set "TDRV=%%i"
    
    for /f "tokens=1" %%i in ('wmic logicaldisk get VolumeSerialNumber^, name ^| findstr /rc:"\<12345678\>"') do set "TDRV=%%i"
    
    for /f %%g in ('wmic path win32_localtime get dayofweek^|findstr /v /r "^$"') do set wday=%%g
    
    wmic qfe list full /format:texttablewsys >C:\updates.txt
    
    wmic qfe list brief | find /N "KB123456"
    
    wmic Path Win32_Battery Get BatteryStatus /format:textvaluelist.xsl | findstr /L "BatteryStatus=2" > NUL
    if errorlevel 1 goto :battery
    
     
  8. adric

    adric MDL Expert

    Jul 30, 2009
    1,251
    1,324
    60
    #8 adric, Feb 23, 2022
    Last edited: Feb 23, 2022
    (OP)
    Code:
    for /f %%g in ('"powershell (Get-Date).DayOfWeek.Value__"') do set wday=%%g
    
    echo wscript.echo WeekDay(Now,VBMonday) >%tmp%\Runit.vbs
    for /f "delims=" %%g in ('cscript /nologo %tmp%\Runit.vbs')  do @set wday=%%g
    
     
  9. geepnozeex

    geepnozeex MDL Junior Member

    Oct 21, 2014
    69
    64
    0
    #9 geepnozeex, Mar 7, 2022
    Last edited: Mar 9, 2022
    forget about Get-WmiObject.
    It doesn't work in powershell 7.
    works like this -
    Code:
    'service','service'|%{(gcim Win32_BaseService -F "name='$_'")|icim -m StopService}
    (gcim Win32_BaseService -F "Name='service'")|icim -m Delete
    gcim Win32_BaseService|? Name -M 'service|service'|icim -m ChangeStartMode -a @{startmode='Disabled'}
    gcim Win32_Process|? Name -M 'process.exe|process.exe'|icim -m Terminate
    
     
  10. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,447
    1,423
    60
    Since 22572.1, WMIC is back as an optional feature