Получить имя заголовка результата команды


When using the Import-Csv cmdlet in PowerShell to import a CSV file, the first line in the file is used as the header by default.

However, occasionally you may want to import a CSV file that has no header row.

Method 1: Use Import-Csv with Custom Header Names

  -Header Team, Points, Assists

This particular example imports the CSV file specified at the path in the $my_file variable and specifies that the header names should be Team, Points and Assists.

Method 2: Use Import-Csv with Numbered Header Names

 = 1..3
Import-Csv  -Header 

This particular example imports the CSV file specified at the path in the $my_file variable and specifies that the header names should simply be the numbers ranging from 1 to 3.

This method is particularly useful when you have a lot of columns and you don’t want to individually specify each column name.

For example, you could use $headers = 1..500 to use the integers ranging from 1 to 500 as the column names for a file that has 500 columns.

Example 1: Use Import-Csv with Custom Header Names

We can use the Get-Content cmdlet to view the content of this file:

Получить имя заголовка результата команды

The file contains three columns that show the team name, points and assists for various basketball players.

Notice that the file does not have a header row.

  -Header Team, Points, Assists

PowerShell Import-Csv no header

We can see that we’re able to successfully import this CSV file and specify the header names that should be used.

Example 2: Use Import-Csv with Numbered Header Names

We can use the Get-Content cmdlet to view the content of this file:

:/>  Безопасное извлечение флешки нужно ли

Получить имя заголовка результата команды

Notice that the file does not have a header row.

 
Import-Csv  -Header 

PowerShell import-csv use numbers as column names

We can see that we’re able to successfully import this CSV file and specify that the numbers 1, 2 and 3 should be used as the header names.

PowerShell: How to Use Export-Csv with No Headers
PowerShell: How to Use Import-Csv and Foreach
PowerShell: How to Merge Multiple CSV Files into One

dir (alias to Get-ChildItem) returns fileinfo, and directoryinfo objects. If you want to see all the members and methods that are available to you, you can pipe to Get-Member. If you’d like to select all properties than what is shown, you can pipe to Select-Object and specify individual properties, or all using *

By default Get-Member hides a few members along with the PSStandardMembers property:

Get-ChildItem -File | Get-Member -Name PSStandardMembers

These members will only reveal if you use the GetMember -Force switch:

Get-ChildItem -File | Get-Member -Name PSStandardMembers -Force

   TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
PSStandardMembers MemberSet  PSStandardMembers {DefaultDisplayPropertySet}
(Get-ChildItem -File)[0].PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
LastWriteTime
Length
Name

I have no clue why the specific Mode property also shows up in table format while it isn’t actually listed here and I would be please if someone can explain this part

The Select-Object will indeed remove this decoration:

Get-ChildItem -File | Select-Object * | Format-Table  -Property @{ e='*'; width = 7 }

PSPath  PSParen PSChild PSDrive PSProvi PSIsCon Mode    ModeWit Version BaseNam Resolve Target  LinkTyp  Length Directo Directo IsReadO FullNam Extensi Name     Exists Creatio Creatio LastAcc LastAcc LastWri LastWri LinkTar UnixFil Attribu
        tPath   Name            der      tainer         houtHar Info    e       dTarget         e               ryName  ry          nly e       on                      nTime   nTimeUt essTime essTime teTime  teTimeU get       eMode     tes
                                                        dLink                                                                                                                   c               Utc             tc
------  ------- ------- ------- ------- ------- ----    ------- ------- ------- ------- ------  -------  ------ ------- ------- ------- ------- ------- ----     ------ ------- ------- ------- ------- ------- ------- ------- ------- -------
Micros… Micros… dir.ps1 P       Micros…   False -a---   -a---   File: … dir     P:\Sta…                     383 P:\Sta… P:\Sta…   False P:\Sta… .ps1    dir.ps1    True 9/7/20… 9/7/20… 9/7/20… 9/7/20… 9/7/20… 9/7/20…              -1 Archive

But you might simply create your own DefaultDisplayPropertySet on each object like this:

$DefaultDisplayPropertySet = [System.Management.Automation.PSPropertySet]::new(
    'DefaultDisplayPropertySet',[string[]]('Name', 'FullName')
)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$DefaultDisplayPropertySet
$Dir = Get-ChildItem -File | Select-Object * | Foreach-Object {
    $_ | Add-Member MemberSet PSStandardMembers $PSStandardMembers -PassThru
}

The object list ($Dir) has still all standard properties available, e.g.:

$Dir.LastWriteTime

Thursday, September 7, 2023 11:07:32 AM

But it only shows just the two default properties when outputted to the display:

$Dir

Name    FullName
----    --------
dir.ps1 C:\FullPath\Test.ps1

Оставьте комментарий