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}
Change all of your Select-Object commands to Select-Object -ExpandProperty PropertyName , to discard the property name / column header.
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.
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.
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.
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
$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 propertyExpression
— script block that returns a valueSee "EXAMPLE 4" here for another example of calculated property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With