Remove Headers

This topic contains 14 replies, has 5 voices, and was last updated by  amit aman 3 months ago.

  • Author
    Posts
  • #98355

    SK
    Participant

    Get-ADUser -Filter *|select GivenName,Name,Enabled,SID | out-file MyDCs.txt

    I get an extract like this.

    GivenName Name Enabled SID
    ——— —- ——- —
    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...
    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...
    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...
    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...

    How can I have only data in the MyDCs.txt file in tab delimited format without the headers

    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...
    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...
    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...
    EFGHIJ ABCDEF... False Z-0-0-00-00000000000-111111...

  • #98358

    amit aman
    Participant

    Output does not look good(format), but this will give you what you desire:

    Get-ADUser -Filter *|%{ "{0,-10}{1,-10}{2,-60}" -f $_.GivenName,$_.Name,$_.Enabled,$_.SID} | out-file MyDCs.txt

    Why don't you use export-csv instead?

    • #98368

      SK
      Participant

      It does not give out the SID's and it is not in tab delimited format

    • #98661

      amit aman
      Participant

      Maybe this is the one you are looking for:

      Get-ADUser -Filter *|%{ "{0,-25}{1,-25}{2,-10}{3,-60}" -f $_.GivenName,$_.Name,$_.Enabled,$_.SID} | out-file abc.txt

      Good Luck!

  • #98362

    Olaf Soyk
    Participant

    Why without headers?

    Get-ADUser -Filter * |
        Select-Object  -Property GivenName,Name,Enabled,SID |
            ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | 
                Select-Object -Skip 1 |
                    out-file MyDCs.txt

    Just a hint: When you limit your AD query with a -Searchbase it will be faster and less effort for the DC. 😉

    • #98365

      SK
      Participant

      Coz I need to import into a table.

    • #98367

      Olaf Soyk
      Participant

      Hmmm ... OK.

    • #98370

      SK
      Participant

      FYI:I went for pipe instead.
      But how do I get rid of the "" around the data.

    • #98377

      Olaf Soyk
      Participant

      But how do I get rid of the "" around the data.

      Hmmm ... usually you don't need if you do it right. And you don't need to remove the header as well. Powershell is able to deal with both, headers and quotes, accordingly.
      What would you like to do with the data? To what kind of table would you like to import them?

    • #98386

      SK
      Participant

      Import it into a SQL table.

  • #98385

    Ron
    Participant
    Get-ADUser ...|select GivenName,Name,Enabled,SID|%{ "{0}`t{1}`t{2}`t{3}" -f $_.GivenName,$_.Name,$_.Enabled,$_.SID}

    There are probably better ways to do whatever you are doing, but this gives you tab delimited output without quotes. If you are looking for a fixed length solution, see Amit's post above and take a look at string formatting help to understand what it is doing. Here's a sample.

    https://ss64.com/ps/syntax-f-operator.html

    • #98458

      SK
      Participant

      Can you please tell me other ways of doing this.

    • #98464

      Olaf Soyk
      Participant

      You might show a little bit of the code you're using. There might be other problems with it. Sometimes you try to solve an issue on the left hand side but actually it is on the right hand side ... you know what I mean!? 😉

    • #98476

      Ron
      Participant

      You mentioned needing to update a table, but without more info, it's hard to suggest a better solution. Is it a spreadsheet? Database table/what type? You are asking for the data in an odd format that shouldn't be required in most cases. If the format required is application specific, then there might not be a better way, but we can't suggest one without knowing more.

  • #98487

    JohnM
    Participant

    Look at Amit's post, there was a small error where he left one variable/placeholder out in the format string. You need four columns and the command only had 3 listed.

    try the following:

    get-aduser -Filter * |%{ "{0,-10}{1,-10}{2,-6}{3,-60}" -f $_.GivenName,$_.Name,$_.Enabled,$_.SID}

    That should produce the output in a fixed width format which you can pipe to Out-File.

    This is the output I produced on my small test:

    John      MyUserID   True  S-1-.-................

    Good Luck
    For Tab Delimited use:

    get-aduser -Filter * |%{ "{0}`t{1}`t{2}`t{3}" -f $_.GivenName,$_.Name,$_.Enabled,$_.SID}

    the above just adds the tab character (`t) into the format which you can pipe to Out-File or Export-CSV.

    Read about the -f operator here:
    https://blogs.technet.microsoft.com/pstips/2014/10/18/powershells-format-operator/

You must be logged in to reply to this topic.