# ExpandProperty Expression

**URL:** <https://forums.powershell.org/t/expandproperty-expression/14635>\
**Category:** PowerShell Help\
**Created:** [July 6, 2020, 7:14am UTC](https://forums.powershell.org/t/expandproperty-expression/14635 "2020-07-06T07:14:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinod-kumar](https://avatars.discourse-cdn.com/v4/letter/v/a87d85/32.png) [@vinod-kumar](https://forums.powershell.org/u/vinod-kumar)\
**Post date:** [July 6, 2020, 7:14am UTC](https://forums.powershell.org/t/expandproperty-expression/14635/1 "2020-07-06T07:14:58Z")

</div>

Hello All,

Am trying to use the ExpandPropery against an expression, but it is throwing up some error

$Val = Get-ChildItem -Path C:\Users -Exclude Default,Public | ? {$_.LastWritetime -gt ‘1/1/2020’} | Select Name, @{Name = “LastWriteTime”; Expression={$_.LastWritetime.Tostring(“D”)}} -ExpandProperty Name ,LastWriteTime

Would like to store the values under $val. What am i doing wrong here ?

---

<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:** [July 6, 2020, 7:44am UTC](https://forums.powershell.org/t/expandproperty-expression/14635/2 "2020-07-06T07:44:40Z")

</div>

Vinod,

When you post code, error messages, sample data or console output format it as code, please.  
In the “ **Text** ” view you can use the code tags “ **CODE** ”, in the “ **Visual** ” view you can use the format template “ **Preformatted** ”. You can go back edit your post and fix the formatting - you don’t have to create a new one.  
Thanks in advance.

```
$Val =
Get-ChildItem -Path C:\Users -Exclude Default, Public |
Where-Object { $_.LastWritetime -gt '1/1/2020' } |
Select-Object -Property Name, @{Name = 'LastWriteTime'; Expression = { $_.LastWritetime.Tostring('D') } }
```

It doesn’t make any sense to select properties twice in one Select-Object.

When you get errors you should always post the complete error message along with your question. Most of the time the error message points to the solution. 😉

---

<div class="post-metadata">

**Author:** ![vinod-kumar](https://avatars.discourse-cdn.com/v4/letter/v/a87d85/32.png) [@vinod-kumar](https://forums.powershell.org/u/vinod-kumar)\
**Post date:** [July 6, 2020, 7:49am UTC](https://forums.powershell.org/t/expandproperty-expression/14635/3 "2020-07-06T07:49:43Z")

</div>

Thanks. So i need $Val to have both values like the username and date. how can it be done.

The error is

Select-Object : Cannot convert ‘System.Object’ to the type ‘System.String’ required by parameter ‘ExpandProperty’.  
Specified method is not supported.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [July 6, 2020, 10:41am UTC](https://forums.powershell.org/t/expandproperty-expression/14635/4 "2020-07-06T10:41:28Z")

</div>

I don’t know where username came into this, but if you simply drop the -expandproperty it’s probably what you’re looking for?

```
$Val = Get-ChildItem -Path C:\Users -Exclude Default,Public | ? {$_.LastWritetime -gt ‘1/1/2020’} |
    Select Name, @{Name = “LastWriteTime”; Expression={$_.LastWritetime.Tostring(“D”)}}
```

Output looked like this in my temp directory.

```
Sample2.csv Friday, June 26, 2020      
Sample3.csv Thursday, June 25, 2020    
Sample4.csv Thursday, June 25, 2020    
Sample5.csv Friday, June 26, 2020      
Sample6.csv Friday, June 26, 2020      
Sample7.csv Friday, June 26, 2020      
Sample8.csv Friday, June 26, 2020      
Sample9.csv Friday, June 26, 2020      
speech.ps1 Wednesday, July 1, 2020    
states.txt Wednesday, June 3, 2020    
statesclean.txt Wednesday, June 3, 2020    
synctest.ps1 Friday, May 1, 2020        
synctest.ps1.old Friday, May 1, 2020        
synctest1.ps1 Friday, May 1, 2020
```

---

<div class="post-metadata">

**Author:** ![vinod-kumar](https://avatars.discourse-cdn.com/v4/letter/v/a87d85/32.png) [@vinod-kumar](https://forums.powershell.org/u/vinod-kumar)\
**Post date:** [July 6, 2020, 10:47am UTC](https://forums.powershell.org/t/expandproperty-expression/14635/5 "2020-07-06T10:47:09Z")

</div>

Thanks. I am trying to write the output to a Textbox inside the form wherein it shows in wrong format

$TextBox1 = New-Object system.Windows.Forms.TextBox  
$TextBox1.multiline = $true  
$TextBox1.WordWrap = $true  
$TextBox1.width = 400  
$TextBox1.height = 200  
$TextBox1.location = New-Object System.Drawing.Point(20,59)  
$TextBox1.Lines = $Val

The output i get it when the form is launched

&nbsp;

@{Name=ABCD; LastWriteTime=22 June 2020}  
@{Name=SER; LastWriteTime=05 May 2020}  
@{Name=Sample; LastWriteTime=05 May 2020}  
@{Name=XYX; LastWriteTime=06 March 2020}  
@{Name=RTYT; LastWriteTime=05 March 2020}  
@{Name=UIOP; LastWriteTime=28 January 2020}

How do we remove the Name and Lastwrite so that it displays only those values

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [July 6, 2020, 10:54am UTC](https://forums.powershell.org/t/expandproperty-expression/14635/6 "2020-07-06T10:54:30Z")

</div>

Then you would want it to be a string, not an object.

```
$Val = Get-ChildItem -Path C:\temp -Exclude Default,Public | ? {$_.LastWritetime -gt ‘1/1/2020’} |
    Select Name, @{Name = “LastWriteTime”; Expression={$_.LastWritetime.Tostring(“D”)}} | Out-String
```

---

<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:29pm UTC](https://forums.powershell.org/t/expandproperty-expression/14635/7 "2024-05-16T20:29:17Z")

</div>

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


