1

The following script appears to show that the width key in a custom property is not used. How can I cause the width of the result to be changed.

# The 'C:\src\t\rrr\' directory contains three (3) subdirectories.
Get-ChildItem | Format-Table -Property Parent,LastWriteTime,FullName
# The 'LastWriteTime' member can be renamed to 'LWT' using a custom property.
Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime}},FullName
# Specifying a 'width' key appears to be ignored.
Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime};width=50},FullName
# The 'LastWriteTime' datetime can be reformatted.
Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime.ToString('MMM yyyy')}},FullName
# However, specifying a 'width' key in the custom property hash has no effect.
Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime.ToString('MMM yyyy')};width=50},FullName
# Omitting the 'name' key will expand the width of the column to the length of the expression, but not to the specified 'width' value.
Get-ChildItem | Format-Table -Property Parent,@{e={$_.LastWriteTime.ToString('MMM yyyy')};width=50},FullName
# Using a 'width' key in a Select-Object property is invalid.
Get-ChildItem | Select-Object -Property Parent,@{n='LWT';e={$_.LastWriteTime};width=50},FullName

Here are the results of the script.

PS C:\src\t\rrr> # The 'C:\src\t\rrr\' directory contains three (3) subdirectories.
PS C:\src\t\rrr> Get-ChildItem | Format-Table -Property Parent,LastWriteTime,FullName

Parent       LastWriteTime       FullName
------       -------------       --------
C:\src\t\rrr 2020-05-11 10:50:39 C:\src\t\rrr\a
C:\src\t\rrr 2023-05-16 12:41:00 C:\src\t\rrr\b
C:\src\t\rrr 2020-05-11 10:51:00 C:\src\t\rrr\c

PS C:\src\t\rrr> # The 'LastWriteTime' member can be renamed to 'LWT' using a custom property.
PS C:\src\t\rrr> Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime}},FullName

Parent       LWT                 FullName
------       ---                 --------
C:\src\t\rrr 2020-05-11 10:50:39 C:\src\t\rrr\a
C:\src\t\rrr 2023-05-16 12:41:00 C:\src\t\rrr\b
C:\src\t\rrr 2020-05-11 10:51:00 C:\src\t\rrr\c

PS C:\src\t\rrr> # Specifying a 'width' key appears to be ignored.
PS C:\src\t\rrr> Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime};width=50},FullName

Parent       LWT                 FullName
------       ---                 --------
C:\src\t\rrr 2020-05-11 10:50:39 C:\src\t\rrr\a
C:\src\t\rrr 2023-05-16 12:41:00 C:\src\t\rrr\b
C:\src\t\rrr 2020-05-11 10:51:00 C:\src\t\rrr\c

PS C:\src\t\rrr> # The 'LastWriteTime' datetime can be reformatted.
PS C:\src\t\rrr> Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime.ToString('MMM yyyy')}},FullName

Parent       LWT      FullName
------       ---      --------
C:\src\t\rrr May 2020 C:\src\t\rrr\a
C:\src\t\rrr May 2023 C:\src\t\rrr\b
C:\src\t\rrr May 2020 C:\src\t\rrr\c

PS C:\src\t\rrr> # However, specifying a 'width' key in the custom property hash has no effect.
PS C:\src\t\rrr> Get-ChildItem | Format-Table -Property Parent,@{n='LWT';e={$_.LastWriteTime.ToString('MMM yyyy')};width=50},FullName

Parent       LWT      FullName
------       ---      --------
C:\src\t\rrr May 2020 C:\src\t\rrr\a
C:\src\t\rrr May 2023 C:\src\t\rrr\b
C:\src\t\rrr May 2020 C:\src\t\rrr\c

PS C:\src\t\rrr> # Omitting the 'name' key will expand the width of the column to the length of the expression, but not to the specified 'width' value.
PS C:\src\t\rrr> Get-ChildItem | Format-Table -Property Parent,@{e={$_.LastWriteTime.ToString('MMM yyyy')};width=50},FullName

Parent       $_.LastWriteTime.ToString('MMM yyyy') FullName
------       ------------------------------------- --------
C:\src\t\rrr May 2020                              C:\src\t\rrr\a
C:\src\t\rrr May 2023                              C:\src\t\rrr\b
C:\src\t\rrr May 2020                              C:\src\t\rrr\c

PS C:\src\t\rrr> # Using a 'width' key in a Select-Object property is invalid.
PS C:\src\t\rrr> Get-ChildItem | Select-Object -Property Parent,@{n='LWT';e={$_.LastWriteTime};width=50},FullName
Select-Object: The width key is not valid.

PS C:\src\t\rrr> $PSVersionTable.PSVersion.ToString()
7.3.4
lit
  • 14,456
  • 10
  • 65
  • 119

0 Answers0