0
votes

Noticed some weird behaviour with PowerShell Get-ChildItem. I do something like this in my script:

New-PSDrive -Name "R" -PSProvider "FileSystem"-Root "\\server\share"
$files = Get-ChildItem "R:/" | Select-Object -Property * -ExcludeProperty VersionInfo
$files | export-csv -Path "output.csv" -Delimiter ";" 

The Get-ChildItem returns an object with properties such as BaseName, FullName, Parent, Root, ...

The problem I am having is that recently (or on some network shares which I mount) the properties of the object returned by Get-ChildItem changed. It used to be this list:

PSPath  PSParentPath    PSChildName PSDrive PSProvider  PSIsContainer   Mode    BaseName    Target  LinkType    Name    FullName    Parent  Exists  Root    Extension   CreationTime    CreationTimeUtc LastAccessTime  LastAccessTimeUtc   LastWriteTime   LastWriteTimeUtc    Attributes

and as of recent (or as mentioned, for certain shared folder, haven't figured that out yet) the following properties were added:

Length  DirectoryName   Directory

In my CSV I now have 3 extra columns in between Name and IsReadOnly the aforementioned 3 properties are added. Does anyone know if this is due to an update of PowerShell or due to the specific server for which the shared folder was mounted?

1
Use the -File parameter on your Get-Childitem command. - AdminOfThings
get-childitem -file | gm still shows these 3 additional properties. You think that it is showing these properties because there are directories amongst the results? - Silver
Ah, get-childitem -Directory | gm does not show these 3 properties. So probably your assumption is correct but I interpreted it inversely. - Silver

1 Answers

0
votes

As pointed out by @AdminOfThings, Get-ChildItem can return multiple object types: DirectoryInfo and FileInfo where only FileInfo has the properties Directory, DirectoryName and Length. Therefore, when exporting the results from Get-ChildItemto a CSV file. This file will only contain the 3 columns if the folder also contained files.

The following documentation describes the behaviour of export-csv in case you submit multiple object with different types Microsoft Powershell6.0:

When you submit multiple objects to Export-CSV, Export-CSV organizes the file based on the properties of the first object that you submit. If the remaining objects do not have one of the specified properties, the property value of that object is null, as represented by two consecutive commas. If the remaining objects have additional properties, those property values are not included in the file.

I will update the answer once I found a solution to make the CSV output consistent.