Convert PS to CMD

Page 3 of 24 FirstFirst 1234513 ... LastLast

  1. Posts : 3,275
    Win10
       #21

    With Registered Organization in a separate VBox, and then without on real PC (both are Win11).

    Convert PS to CMD-org-valid.pngConvert PS to CMD-org-none2.png
      My Computers


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #22

    das10 said:
    With Registered Organization in a separate VBox, and then without on real PC (both are Win11) . . .
    Thanks for performing an actual test and posting the results @das10.
    Last edited by Paul Black; 22 Feb 2022 at 05:04.
      My Computer


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #23

    Well, I have REALLY had some fun [ and learnt a lot ] manipulating data and output.

    This thread started out as a general PowerShell to CMD Prompt or Batch Script conversion question.

    Then I thought about it, and I decided to turn this into a project for myself where I could gain knowledge and experience for future use while also creating something informative and useful, and as it turned out, it involved some quite complex calculations and data manipulations.

    I might actually start a new thread and post it for general use.

    Here are some of the things that I performed.

    • Converted flat numerical data output [ Bytes ] into Megabytes [ Calculation ] with a comma for the thousands and set to no decimal places, with the text MB after the formatted value..
    • Converted flat numerical data output [ Bytes ] into Gigabytes [ Calculation ] with a decimal point and set to two decimal places, with the text GB after the formatted value.
    • This will make you laugh. One output showed 'Normal boot', and my OCD kicked in, so I did no more than change it to 'Normal Boot' using the replacement method if it was found.
    • Specified specific text output dependant on the results returned.
    • I have separated values and text output.
    • I have separated values and text output, and then injected further values or text.
    • I have extracted values and text from a string output rather than having the full output of the values or text.
    • I have used a variety of commands such as findstr, Replace, -Like, -ExpandProperty, [Math]::Round, .ToString, Where, -eq 'True', to name a few.
    • I have used two variables in a single command to produce the required output.

    There were MANY different types of Commands, and they all worked, but when I looked at them, I noticed that there were small differences in the way that I had created [ Built ] them, therefore making it harder to to read because of the lack of continuity.

    Anyway, I spent some time looking at them, and then I spent some more time experimenting with them, and I came to a very simple working solution, obviously all part of the learning process. I figured out that when converting PS to CMD Prompt or Batch Script, that you ONLY need to use speech marks [ Quotes ] " at the very beginning and the very end of the command, and use an apostrophe ' inside for text, labels, or mathematical operators etc. Plus, I noticed that this appeared to allow the code to run quicker.

    Anyway, before I write a novel, I will leave it there, and I hope that this will help anyone that come across, or is following this thread.
    A special thanks goes to @das10 for his time, expertise, and great input, it is appreciated.
    Last edited by Paul Black; 22 Feb 2022 at 07:23.
      My Computer


  4. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #24

    Hello @das10,

    das10 said:
    Code:
    PowerShell Write-Host "' OS Manufacturer      :'(Get-CimInstance -ClassName Win32_OperatingSystem).Manufacturer"
    Thank you for the update.

    When I was streamlining the Commands, I did actually do away with the Select portion in the Commands with a single selection where possible, replacing it with () and the . as shown in your code above. That among other changes makes it so much more readable.

    Thanks again.
      My Computer


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #25

    Just when I thought that I had pretty much everything covered, I came across one I just can NOT seem to get right.

    I have this which works . . .

    Code:
    
    PowerShell "[System.Environment]::OSVersion | Format-Table -Autosize @{L='Platform';E={($_.Platform)};A='Left'}, ServicePack, Version, VersionString | Format-Table -Autosize | Out-String -Width 1000"
    
    Platform ServicePack Version      VersionString
    -------- ----------- -------      -------------
    Win32NT              10.0.18363.0 Microsoft Windows NT 10.0.18363.0
    

    Now I want to add Not Available to the ServicePack like this . . .

    Code:
    
    Platform ServicePack   Version      VersionString
    -------- -----------   -------      -------------
    Win32NT  Not Available 10.0.18363.0 Microsoft Windows NT 10.0.18363.0

    This is what I have tried in PS ISE [ I can convert it after it works ]. I have just used the ServicePack Parameter to get it to work, I can then add it to the original above . . .

    Code:
    
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {($_ServicePack)}
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {($_ServicePack -eq $_ServicePack)}
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {Write-Host ($_ServicePack)}
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack}
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq $True) {$_ServicePack -eq 'Not Available'} else {$_ServicePack}
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available' | select $_ServicePack -ExpandProperty $_ServicePack} else {$_ServicePack -eq $_ServicePack}
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack}; select $_ServicePack -ExpandProperty $_ServicePack
    [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack}; select $_ServicePack -Expand $_ServicePack
    
    
    
    
    
    
    
    
    Code:
    
    [System.Environment]::OSVersion; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack}
    
    Platform ServicePack Version      VersionString                    
    -------- ----------- -------      -------------                    
     Win32NT             10.0.18363.0 Microsoft Windows NT 10.0.18363.0
    
    True

    I tried to find out what the cell actually contained using the following in PS ISE . . .

    Code:
    
    $MyString = '[System.Environment]::OSVersion.ServicePack'
    if($MyString) {            
        Write-Host "The String is NOT EMPTY"           
    } else {            
        Write-Host "The String is EMPTY or NULL"           
    }
    
    
    
    $MyString = '[System.Environment]::OSVersion.ServicePack'
    $a = "[System.Environment]::OSVersion.ServicePack"
    if ($a) { 
        Write-Output "Variable contains Value" 
    } else { 
        Write-Output "Variable is Empty"
    }
    
    
    
    $MyString = '[System.Environment]::OSVersion.ServicePack'           
    if([String]::IsNullOrWhiteSpace($MyString)) {            
        Write-Host "The String is NULL or EMPTY or it has WHITESPACE"           
    } else {            
        Write-Host "The String is NOT EMPTY"           
    }

    It reports that the String is NOT Empty, so what is it?

    Code:
    
    $a = '[System.Environment]::OSVersion.ServicePack'
    if ($a) { 
        Write-Output "Variable contains Value" 
    } else { 
        Write-Output "Variable is Empty"
    }
    
    Variable contains Value

    Thanks.
    Last edited by Paul Black; 25 Feb 2022 at 09:49.
      My Computer


  6. Posts : 3,275
    Win10
       #26

    I am afraid, I have no idea. Even running various get information commands like shown below, I can't see amy mention of just the "ServicePack" information like it shows with your command.
    eg:
    Code:
    Get-ComputerInfo -Property *
    or
    Get-CimInstance -Class CIM_OperatingSystem -ErrorAction Stop | Select *
      My Computers


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #27

    Thanks @das10,

    das10 said:
    I am afraid, I have no idea. Even running various get information commands like shown below, I can't see any mention of just the "ServicePack" information like it shows with your command.
    eg:
    Code:
    Get-ComputerInfo -Property *
    or
    Get-CimInstance -Class CIM_OperatingSystem -ErrorAction Stop | Select *
    I couldn't find anything else that showed it either. To be honest, I was just interested for the future, in case I ever came across a similar scenario.

    I am NOT actually going to include it in the list of Commands anyway, it was just the fact that it was there and I thought I would give it a go.

      My Computer


  8. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #28

    Anyone that knows me, knows that I am NOT someone who will be beaten.

    Therefore, I decided to have ANOTHER go and see if I could crack it, and BINGO.

    For anyone that is interested, here it is . . .

    Equal to ' '
    Code:
    
    PowerShell "[System.Environment]::OSVersion | Format-Table -Autosize @{L='ServicePack';E={;if($_.ServicePack -eq '') {'Not Available'} else {'xxx'}}}"
    
    ServicePack
    -----------
    Not Available

    NOT Equal to ' '
    Code:
    
    PowerShell "[System.Environment]::OSVersion | Format-Table -Autosize @{L='ServicePack';E={;if($_.ServicePack -ne '') {'Not Available'} else {'xxx'}}}"
    
    ServicePack
    -----------
    xxx

    I used xxx in the else statement as a way to show that it worked as there is NO actual value available. You would obviously replace the xxx with $_.ServicePack.

      My Computer


  9. Posts : 3,275
    Win10
       #29

    Paul, on a Windows XP VBox, this is what I got:
    Code:
    C:\>%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe "[System.Environment]::OSVersion
    
    Platform ServicePack         Version             VersionString
    -------- -----------         -------             -------------
    Win32NT Service Pack 3      5.1.2600.196608     Microsoft Window...
    
    
    
    C:\>%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe "[System.Environment]::OSVersion | Format-Table -Autosize @{L='ServicePack';E={;if($_.ServicePack -ne '') {'Not Available'} else {'$_.ServicePack'}}}"
    
    ServicePack
    -----------
    Not Available
    Also, using P_Ise in the XP VBox:
    Code:
    Write-Host `n--------------------------- ENV Platform---------------------------
    [environment]::OSVersion.Platform 
    Write-Host `n--------------------------- ENV ServicePack------------------------
    [environment]::OSVersion.ServicePack
    Write-Host `n--------------------------- ENV Version----------------------------
    [environment]::OSVersion.Version
    Write-Host `n--------------------------- ENV VersionString----------------------
    [environment]::OSVersion.VersionString 
    --------------------------- ENV Platform---------------------------
    Win32NT
    
    --------------------------- ENV ServicePack------------------------
    Service Pack 3
    
    --------------------------- ENV Version----------------------------
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      1      2600   196608  
    
    --------------------------- ENV VersionString----------------------
    Microsoft Windows NT 5.1.2600 Service Pack 3


    ======================================================================
    Also using P_Ise on XP and Win11:

    Code:
    XP VBox:
    
    $MyString = [System.Environment]::OSVersion.ServicePack
    if($MyString -eq "") {            
        Write-Host "The String is EMPTY"           
    } else {            
        Write-Host $MyString
    }
    
    =
    
    Service Pack 3
    
    _______________________________________________________
    
    and on Win11:
    
    $MyString = [System.Environment]::OSVersion.ServicePack
    if($MyString -eq "") {            
        Write-Host "The String is EMPTY"           
    } else {            
        Write-Host $MyString
    }
    
    =
    
    The String is EMPTY
    
    ________________________________________________________
      My Computers


  10. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #30

    That's good then, thanks.

    I have an interesting one that I can NOT seem to figure out.

    This works in PS ISE . . .

    Code:
    
    Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}}
    
     Name                  : Wi-Fi
     Interface Description : Qualcomm Atheros AR9285 Wireless Network Adapter
     Connector Present     : True

    This works in CMD Prompt . . .

    Code:
    
    PowerShell "Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}}"
    
    
     Name                  : Wi-Fi
     Interface Description : Qualcomm Atheros AR9285 Wireless Network Adapter
     Connector Present     : True

    I just can NOT get it to work in a Script . . .

    Code:
    
    PowerShell "Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}}"
    
    
    '"Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}}"' is not recognized as an internal or external command,
    operable program or batch file.

    Am I having a Senior moment?
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 12:58.
Find Us




Windows 10 Forums