# Select-Object - NoteProperty & Dropping Object with empty/null property

**URL:** <https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530>\
**Category:** PowerShell Help\
**Created:** [September 26, 2022, 10:04am UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530 "2022-09-26T10:04:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![droid](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@droid](https://forums.powershell.org/u/droid)\
**Post date:** [September 26, 2022, 10:04am UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/1 "2022-09-26T10:04:18Z")

</div>

I have a couple of problems I’m encountering when using Select-Object with an AWS cmdlet (Get-IAMRole) and hope someone could explain where I’m going wrong?

1. When selecting object properties that are piped through from “Get-IAMRole” I’ve found one of them is a “NoteProperty”. I’ve been able to access this using the “ExpandProperty” parameter. The output provides me with a date and time. However, I would prefer to only have the date but I’ve been unable to separate them. Any suggestions on how I can can achieve this? I have tried but failed so far.

2. My full script simply gets the objects (IAM Roles), pipes them through to select-object where I select the information I want, then outputs this information to a .csv file. I’ve found that if one of the object properties is empty/null it doesn’t appear in the output to the .csv. I believe I’ve narrowed this down to the select-object cmdlet, rather than the export to .csv, as removing the property (-ExpandProperty “RoleLastUsed”) from select-object gives me the expected results. Any advice on how I can prevent Select-Object from dropping the object if one of the selected properties is empty/null?

Below is part of the script. I’ve already created a .txt file with the role names

```auto
$RoleNames = Get-Content D:\temp\RoleNames_$account.txt

foreach ($name in $RoleNames){
    # ExpandProperty is required to access the last used date.
    Get-IAMRole -Credential $creds -rolename $name |`
    Select-Object -Property RoleName, CreateDate, Arn, RoleId, Path, Description -ExpandProperty "RoleLastUsed" |`
    export-csv D:\temp\RoleDetails_$account.csv -NoTypeInformation -Append 
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 26, 2022, 10:18am UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/2 "2022-09-26T10:18:49Z")

</div>

jim,  
Welcome to the forum. 👋🏽

If you need to use `-ExpandProperty` it indicates that the property is either an array or has sub properties.

Since I don’t have any experience with AWS and no access to it neither I cannot tell what `RoleLastUsed` could be. With your describtion I’d suspect it could have sub properties.

How does it look when you run

```auto
Get-IAMRole -Credential $creds -rolename $name |
    Select-Object -ExpandProperty RoleLastUsed

```

for only one `$name` ?

---

<div class="post-metadata">

**Author:** ![droid](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@droid](https://forums.powershell.org/u/droid)\
**Post date:** [September 26, 2022, 10:31am UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/3 "2022-09-26T10:31:52Z")

</div>

Hello Olaf,

thanks for replying.

This is the output I receive “8/31/2021 0:40”. Note it provides the date then time.

```auto
   TypeName: Amazon.IdentityManagement.Model.RoleLastUsed

Name MemberType Definition                      
---- ---------- ----------                      
Equals Method bool Equals(System.Object obj)  
GetHashCode Method int GetHashCode()               
GetType Method type GetType()                  
ToString Method string ToString()               
LastUsedDate Property datetime LastUsedDate {get;set;}
Region Property string Region {get;set;}        

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 26, 2022, 10:47am UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/4 "2022-09-26T10:47:19Z")

</div>

> [@droid](#):
>
> However, I would prefer to only have the date

This should do it actually then:

```auto
$RoleNames = Get-Content D:\temp\RoleNames_$account.txt
$Result = 
foreach ($name in $RoleNames){
    Get-IAMRole -Credential $creds -rolename $name |
    Select-Object -Property RoleName, CreateDate, Arn, RoleId, Path, Description,
        @{Name = 'LastUseDate'; Expression = {$_.RoleLastUsed.LastUsedDate.Date}} 
}
$Result |
Export-Csv -Path "D:\temp\RoleDetails_$account.csv" -NoTypeInformation 

```

If the `.Date` sub property is not available you could use the `ToString()` method instead …

```auto
@{Name = 'LastUseDate'; Expression = {$_.RoleLastUsed.ToString('dd-MM-yyyy')}

```

for example. … or whatever format you prefer. 😉

---

<div class="post-metadata">

**Author:** ![droid](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@droid](https://forums.powershell.org/u/droid)\
**Post date:** [September 26, 2022, 12:47pm UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/5 "2022-09-26T12:47:10Z")

</div>

Thanks. I’ll give this try later today, I have to prioritise some other work at the moment.

Any thoughts on the missing .csv outputs? Do you think your previous suggestions will solve that too?

---

<div class="post-metadata">

**Author:** ![droid](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@droid](https://forums.powershell.org/u/droid)\
**Post date:** [September 27, 2022, 9:27am UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/6 "2022-09-27T09:27:06Z")

</div>

Hello,

I was able to do some testing…

Using `@{Name = 'LastUseDate'; Expression = {$_.RoleLastUsed.LastUsedDate.Date}}` solved the issue of RoleNames with null `LastUsedDate` properties not being passed through to the .csv. However, it didn’t remove the time from the `LastUsedDate` output. Instead, the output was displayed like this - `9/27/2022 0:00` - where the times are all `0:00`. While this isn’t perfect, the fact that I’m not losing any of the RoleName objects without a `LastUsedDate` property is good for me.

Using `@{Name = 'LastUseDate'; Expression = {$_.RoleLastUsed.ToString('dd-MM-yyyy')}` was the same as above except it didn’t provide any properties for `LastUsedDate`. While all the RoleNames existed the `LastUseDate` column was empty.

Do you happen to know why changing from using the `ExpandProperty` parameter to the array stopped me from losing RoleName objects with no `LastUsedDate` property?

Also, thanks for you help! I’ll mark your previous response as the solution as it has fixed the main issue I was encountering.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 27, 2022, 9:31am UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/7 "2022-09-27T09:31:30Z")

</div>

> [@droid](#):
>
> Do you happen to know why changing from using the “ExpandProperty” parameter to the array stopped me from losing RoleName objects with no “LastUsedDate” property?

No. Sorry. 🤷🏽‍♂️

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/select-object-noteproperty-dropping-object-with-empty-null-property/20530/8 "2024-05-16T20:16:35Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


