Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select-Object returns the first result as my column name instead of as a result

Tags:

powershell

The following script checks mailboxes in a specified OU for users with the default permission set to Owner. I have tried everything to add the column "Name" from the variable $Mailbox to the Select-Object results in the following code. The result will add a column header with the first results name as the header. If there are multiple results the first name found will be the column header with the column blank for the others. Is there a way to add the "Name" column from the variable $Mailbox to the Select-Object results?

$AllMailBox = Get-Mailbox   -OrganizationalUnit "XXXXXXXXXX.com/DomainUsers/Users" -resultsize unlimited
ForEach ($MailBox in $AllMailbox) {Get-mailboxfolderpermission $Mailbox | Where-object {$_.User -match "Default" -AND $_.AccessRights -match "Owner"} | Select-Object  Identity, AccessRights, $MailBox.Name} 
like image 692
Harry Avatar asked Oct 29 '15 07:10

Harry


People also ask

How do I remove a column name in powershell?

Change all of your Select-Object commands to Select-Object -ExpandProperty PropertyName , to discard the property name / column header.

What does select-Object do in Powershell?

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.

Where Object cmdlet is used to _?

The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a particular ID, or computers that use a particular version of Windows.

How do I get the value of an Object in powershell?

The most common way to get the values of the properties of an object is to use the member access operator ( . ). Type a reference to the object, such as a variable that contains the object, or a command that gets the object. Then, type the operator ( . ) followed by the property name.


2 Answers

Instead of

Select-Object  Identity, AccessRights, $MailBox.Name} 

Add this

Select-Object  Identity, AccessRights, @{l="Name";e={$MailBox.Name}}} 

where l stands for Label and e stands for Expression.

You can read more about custom tables here https://technet.microsoft.com/en-us/library/ee692794.aspx?f=255&MSPPError=-2147217396

like image 89
Edijs Perkums Avatar answered Oct 04 '22 11:10

Edijs Perkums


$MailBox.Name lives in outer scope. It isn't a valid property of item returned from Get-mailboxfolderpermission.

You can use calculated property in Select-Object to add additional data to the result:

{Get-mailboxfolderpermission $Mailbox | Where-object {$_.User -match "Default" -AND $_.AccessRights -match "Owner"} | Select-Object  Identity, AccessRights,@{Name="Name"; Expression={$MailBox.Name}}

Calculated property is declared by hash table with keys:

  • Name — the name of the property
  • Expression — script block that returns a value

See "EXAMPLE 4" here for another example of calculated property.

like image 41
Kirill Avatar answered Oct 04 '22 12:10

Kirill