Using PoSH to get meaning, strange results

I am using PoSH to query our Exchange server to return the largest mailbox to users.

My PoSH:

$test = Get-MailboxDatabase MBX_* -Status | select Name,@{Name="NumberofUsers";Expression={(Get-Mailbox -resultsize unlimited -Database $_.name).Count}} | Sort -Property NumberofUsers | Select Name -First 1

      

This works in the console and returns:

Name
----
MBX_2

      

However, when I run it this way in a script and return a value, it looks like this:

$test = Get-MailboxDatabase -Status MBX_*| select Name,@{Name="NumberofUsers";Expression={(Get-Mailbox -resultsize unlimited -Database $_.name).Count}} | Sort -Property NumberofUsers | Select Name -First 1
Write-Host "Using $test"

Using @<Name=MBX_2>

      

Why does this include symbols @<Name=>

and how can I prevent it? I need to be able to only get the return value "MBX_2" for the next part of my script, and I'm confused as to how to do this ...

+3


source to share


1 answer


You select one property of the object, but still pass the object. What you need to do is use a -ExpandProperty

cmdlet parameter Select-Object

. Change the command Select

to the following:

| Select -ExpandProperty Name -First 1

      



This should give you the results you want.

+5


source







All Articles