Results 1 to 10 of 10
  1. #1
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,644
    Thanks
    37
    Thanked 224 Times in 199 Posts

    Using PowerShell maths in a BATch file

    The Windows Management Instrumentation query
    wmic diskdrive get size
    gives the size of the disk drive(s) in bytes, whose value is invariably rather greater than the ~2 GiB =2,147,483,647 allowable in a BATch file variable.

    To convert the bytes value to a number in gibibytes, one method is to use PowerShell:

    :: this is a nominal 80 GB hard drive
    set bytes=80023749120

    for /f %%a in ('powershell.exe %bytes% / 1GB') do set gibibytes=%%a
    echo %bytes% %gibibytes% GiB

    but this gives a value with 15 significant figures: 74.5279240608215 !

    If the FOR command is modified to

    for /f %%a in ('powershell.exe %bytes% / 1GB -as [int]') do set gibibytes=%%a


    the result is given as an integer: 75
    which is rather more useful.

    My question is: being unable to find much about the PowerShell 'as' parameter by searching (perhaps not surprisingly!) is there any variant of it such as (for example) '-as [two decimal places]' ?
    Last edited by BATcher; 2019-03-11 at 06:18.
    BATcher

    If it goes right, nobody remembers;
    if it goes wrong, nobody forgets.

  2. #2
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    34
    Thanks
    0
    Thanked 13 Times in 11 Posts
    Batcher, try changing: ('powershell.exe %bytes% / 1GB -as [int]')
    To: ('powershell.exe [math]::Round(%bytes% / 1GB^,2^)')

    Cliff
    Cliff

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,165
    Thanks
    451
    Thanked 1,656 Times in 1,488 Posts
    BATcher,

    Here's another option:
    powershell.exe "'{0:#.00}' -f (%bytes% / 1GB)"

    Returns: 74.53

    HTH
    May the Forces of good computing be with you!

    RG
    (still RetiredGeek on AskWoody.com)

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,165
    Thanks
    451
    Thanked 1,656 Times in 1,488 Posts
    BATcher,

    If you want to get powershell to do all the work:

    powershell.exe "$('{0:#.00}' -f ((Get-Disk -number 1).Size / 1GB))"

    But then again why not just program it all in PowerShell ... Power to the Shell!

    HTH
    May the Forces of good computing be with you!

    RG
    (still RetiredGeek on AskWoody.com)

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,644
    Thanks
    37
    Thanked 224 Times in 199 Posts
    Thanks, both - I'll try all those options when I'm back in work again!

    RG: love your joke, but I would need another lifetime to cope with that!
    BATcher

    If it goes right, nobody remembers;
    if it goes wrong, nobody forgets.

  6. #6
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,644
    Thanks
    37
    Thanked 224 Times in 199 Posts
    One interesting result with the first of the two nominal 80 GB drives:

    79999073280 75 GiB my [as int] value
    79999073280 74.5 GiB Cliff's (supposedly) two decimal places value
    79999073280 74.50 GiB RG's two decimal places value


    All Cliff's other results for my BATch file table, built from the results from about 60 PCs:
    :: size in bytes GB GiB
    :: ------------- --- ---
    ::## 79999073280 80 75
    ::## 80023749120 80 75
    ::## 159998146560 160 149
    ::## 160039272960 160 149
    ::## 249994321920 250 233
    ::## 250056737280 250 233
    ::## 300082890240 300 279
    ::## 500105249280 500 466
    ::## 500107714560 500 466

    give the correct two decimal places.

    Thanks again!

    RG: for interest, what does the 0: in '{0:#.00}' represent? I presume the -f means calculate.
    Last edited by BATcher; 2019-03-13 at 02:56.
    BATcher

    If it goes right, nobody remembers;
    if it goes wrong, nobody forgets.

  7. #7
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,165
    Thanks
    451
    Thanked 1,656 Times in 1,488 Posts
    BATcher,

    -f = format.

    The 0: sets it as a number format.

    HTH
    May the Forces of good computing be with you!

    RG
    (still RetiredGeek on AskWoody.com)

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  8. #8
    3 Star Lounger
    Join Date
    Dec 2009
    Location
    Surrey, UK
    Posts
    309
    Thanks
    14
    Thanked 76 Times in 64 Posts
    Quote Originally Posted by BATcher View Post
    RG: for interest, what does the 0: in '{0:#.00}' represent? I presume the -f means calculate.
    I believe the first 0 (before the colon ) indicates an operation on the first element in an array - for a single variable it must be element 0. If you have an array s = 12.345, 23.456, 34.567 then
    $('{0:#.00}' -f $s) gives 12.34
    $('{1:#.0}' -f $s) gives 23.5 etc

    I have only just learnt this since seeing this thread! Martin

  9. The Following User Says Thank You to mngerhold For This Useful Post:

    RetiredGeek (2019-03-13)

  10. #9
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,644
    Thanks
    37
    Thanked 224 Times in 199 Posts
    Thank you! I have learned 110% more about PowerShell than I knew previously...!
    BATcher

    If it goes right, nobody remembers;
    if it goes wrong, nobody forgets.

  11. #10
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,165
    Thanks
    451
    Thanked 1,656 Times in 1,488 Posts
    mngerhold,

    Well who knew, certainly not me! Thanks for that insight.

    Code:
    Clear-Host
    
    #--- Create an array ---
    $x = 12.34, 34.21
    
    #--- Output the array in a single string ---
    " '{0:#.00}' First Number '{1:#.00}' Second Number" -f $x
    Formatted Array.JPG

    HTH
    May the Forces of good computing be with you!

    RG
    (still RetiredGeek on AskWoody.com)

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •