We help IT Professionals succeed at work.

Select String - If Else Statement - Powershell

735 Views
Last Modified: 2017-04-12
Hi All,

I have this line of code in my powershell script:

$a=$mailboxtomigrate.totalitemsize | Select-String "GB", "MB", "KB" | Out-String

Open in new window


is there way to add if else statement to it? the result is "MB" or "KB" I want to covert it to GB, how do I do this?

thank you in advance
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
Will the native method not work for you?
$mailboxtomigrate.totalitemsize.value.togb()

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
the above will only work directly in the exchange management shell, not in powershell remoting
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
$a = (1 * $a) / 1GB

Open in new window

CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Kelly GarciaSenior Systems Administrator

Author

Commented:
Hi Sirbounty,

this looks great, are the results in GB?
Kelly GarciaSenior Systems Administrator

Author

Commented:
Hi Sirbounty,

this is a fantastic script!
 

how do I strip of the size and ------, as I only need the numbers?
CERTIFIED EXPERT
Top Expert 2007

Commented:
Glad it helped.
You should be able to format the numbering using something like this, and just run it through another select with the expand parameter to strip the header:

$mailboxtomigrate.totalitemsize | select @{label='Size' ; expression={ "{0:N2}" -f  (($_.split('(')[1].split(' ')[0].replace(',',''))/1024/1024/1024) }} | select-object -expand Size

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
Fantastic!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.