I’m having a strange situation when creating some statistics:
consider a folder with log files
I need to create 2 reports:
1st report: get the year and month when the files were last written to, group them and show the count of the files
2nd report: get the first 2 characters of each file name, group the results and show the count
In this example I only included 11 files, but my folder has a few thousands.
When running the last 2 commands individually, I get the expected result (column headers and correct info):
When I select both commands and execute them together, the last one does not generate column headers and also the ‘Location Code’ column is empty.
I’m having problems understanding why.
Powershell does a lot of “optimization” for you under the hood. And sometimes it does a little bit too much. I assume you want to output the results only to the console anyway, right? You could use a format cmdlet to output the results like you want it.
Powershell is implicitly sending two different objects to format-table, but format-table can only display one set of columns. Unless there is a format file that tells how to display the first object. Here’s an odd workaround where you put get-date at the beginning of the script. Explicitly running format-table more than once is another workaround. If there were 5 properties in the first object, it would implicitly go to format-list and wouldn’t be a problem.
[pscustomobject]@{name = 'Joe'; address = 'Here'}
[pscustomobject]@{name = 'Joe'; phone = '8675309'}
name address
---- -------
Joe Here
Joe
get-date
[pscustomobject]@{name = 'Joe'; address = 'Here'}
[pscustomobject]@{name = 'Joe'; phone = '8675309'}
Monday, March 11, 2019 9:48:54 AM
name : Joe
address : Here
name : Joe
phone : 8675309
[pscustomobject]@{name = 'Joe'; address = 'Here'; phone='123'; street = 'johnson'; town='springfield'}
[pscustomobject]@{name = 'Joe'; phone = '8675309'; address = 'here'; extra = 'this'};
name : Joe
address : Here
phone : 123
street : johnson
town : springfield
name : Joe
phone : 8675309
address : here
extra : this