How to split the results of a get command with delimiter

Welcome Forums General PowerShell Q&A How to split the results of a get command with delimiter

This topic contains 8 replies, has 4 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #104884

    Participant
    Points: 0
    Rank: Member

    Dear community,

    I am using the get-smbconnection cmdlet and would like to know how is it possible to split the 6 columns that its result returned into 6 separate outputs with a delimiter

    This will help me to display at will any of the 6 columns, because they will have a clear delimiter between each column and a row per result. An array in shot 🙂

    An example of this cmdlet and its output:

    PS C:\Windows\system32> get-smbconnection
    
    ServerName      ShareName           UserName           Credential                Dialect NumOpens
    ----------      ---------           --------           ----------                ------- --------
    Server1         ShareA              DOMAIN\userA       DOMAIN\userA              3.0     1
    Server2         ShareB              DOMAIN\userB       DOMAIN\userB              3.0.2   3
    

    I would like to be able to display it as an array with columns and delimiters.

    Let me know if I am not entirely clear

    Thanks a lot in advance

  • #104887

    Participant
    Points: 59
    Rank: Member

    May I suggest that there are better tools to display graphical tabular information rather than the text screen, such as Microsoft Excel.
    You can simply pipe the output of the get-smbconnection cmdlet to export-csv
    In the text screen, you can pipe the output to Format-Table with the -AutoSize switch for slightly better display

  • #104890

    Participant
    Points: 0
    Rank: Member

    Hello Sam,

    Many thanks for your reply and time.

    I forgot to mention that I intend to use that in a script on a script executing tool that will run it on multiple computers and therefore it needs to return the results to the standard output of the console

    Thanks

  • #104893

    Participant
    Points: 134
    Helping Hand
    Rank: Participant

    Another cool way to display structured data produced by a Powershell script is Out-GridView. Therefor you don't even need any other tool installed. And if needed you can make a choice in this gridview and perform further steps with the chosen data.

  • #104894

    Participant
    Points: 159
    Helping Hand
    Rank: Participant

    From what you've said, it seems like the issue is more a need to store the data rather than to split up the columns.

    There are several ways to go about this, but the simplest is just to export the objects to a text-based format, like CSV or json. Example:

    # Uses a comma-delimited file by default, pass a string to -Delimiter to change this behaviour
    Get-SmbConnection | Export-Csv -Path '\\path\to\file.csv' -NoTypeInformation # Export
    $SMB = Import-Csv -Path '\\path\to\file.csv' # Import; if non-default delimiter, specify delimiter
    
    # Fixed format, but works better with nested datasets
    Get-SmbConnection | ConvertTo-Json | Set-Content -Path '\\path\to\file.json' # Export
    $SMB = Get-Content -Path '\\path\to\file.json' | ConvertFrom-Json # Import
    • #104923

      Participant
      Points: 0
      Rank: Member

      Thank you all

      I think this is not yet the goal of what I am looking and I think it is due to a misexplaination of myself, so I will try better

      The output of my script needs to be in the console, it cannot be in a file. And import it as suggested by Joel is not ideal since I am losing the delimiter needed.

      The goal is more to split the columns returned by the cmdlets, in new columns displayed in the console but that have a delimiter.

      As my tool will return the results of this script as follow on its UI, I need to be able to give him the information with a delimiter it can take into account, each row will be a different computer and in each column I need to have all the rows the cmdlet returned for each column for the cmdlet answer (like 3 lines in there were 3 smb shares open).
      Image

      Hope I am a bit clearer

      Thanks again all for your great help !

    • #104924

      Participant
      Points: 134
      Helping Hand
      Rank: Participant

      The output of my script needs to be in the console ...

      May I ask why? What's the goal? What's wrong with Format-Table -Autosize? Did you try Out-GridView?

    • #104947

      Participant
      Points: 159
      Helping Hand
      Rank: Participant

      Rather than export, you could instead make use of ConvertTo-Csv, which will return the same thing, just in the console (delimiters and all).

  • #105074

    Participant
    Points: 0
    Rank: Member

    Thank you Joel,

    ConvertTo-Csv did the trick !

    I added the -notypeinformation | select -skip 1 at the end to remove header from my results since I don't need them

    Thanks a lot all !

    The final code is as follow

    $result= get-smbconnection | select servername,sharename,credential,dialect | convertto-csv -notypeinformation | select -skip 1
    Write-Output $result

The topic ‘How to split the results of a get command with delimiter’ is closed to new replies.