Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Select-Object property to expand all the fields in the power shell command

Tags:

powershell

Get-ChildItem -Path E:\Server_Data\data\2015 -Recurse –File -include "*.txt","*.csv" | Where-Object {$_.Name -like "*transaction*"} | Select-Object -ExpandProperty FullName,LastWriteTime

I'm trying to list all files in a folder using Get-ChildItem and Select-Object property. When I try to use FullName variable to list the fully qualified file name, the file name is getting truncated. Tried to use -ExpandProperty to get fully qualified file name. It works for one field but if I try to list both FullName and LastWriteTime, it's not working.

The output from the power shell command will be used in MS SQL Server to load the file names into a specific table.

Please suggest proper syntax for my purpose. Appreciate your help!

like image 438
Luqman ChettiyarThodi Avatar asked Sep 28 '15 10:09

Luqman ChettiyarThodi


1 Answers

Depending on your use case and input, one way to accomplish this is by having two Select-Object cmdlets in your pipeline, one to define an array of properties, and one to expand them:

PS C:\> $Name,$DisplayName,$Status = Get-Service 
  | Select-Object -First 1 -Property @{
    Name = "MyProperties"
    Expression = { $_.Name,$_.DisplayName,$_.DisplayName }
  } | Select-Object -ExpandProperty MyProperties
like image 117
Mathias R. Jessen Avatar answered Oct 12 '22 23:10

Mathias R. Jessen