News, tips, advice, support for Windows, Office, PCs & more
Home icon Home icon Home icon Email icon RSS icon

We're community supported and proud of it!

  • Powershell script needs minor tweaks: Remove header/extra spaces in output file

    Home » Forums » Developers, developers, developers » DevOps Lounge » Powershell script needs minor tweaks: Remove header/extra spaces in output file

    Author
    Topic
    #2395689

    I’m slowly learning to write PS scripts, mostly through examples. I modified a script I found online to retrieve the usernames from two OUs in AD, sort them, and output the results to a text file. Here’s what I use:

    $ous = ‘OU=OU1,DC=xxxx,DC=xxxx’,’OU=OU2,DC=xxxx,DC=xxxx’
    $ous| ForEach { Get-ADUser -Filter * -SearchBase $_ | Select SAMAccountName} | sort SAMAccountName | out-file C:\Users\Public\Documents\usernames.txt

    It works great, except for two small inconveniences:

    1. The output has a header.
    2. There are several spaces after each entry.

    The file is being read into a VB program I’m writing, so I can work around both issues. But I’d like to just have a clean file, especially since I think it should be an easy fix for both. I’ve done some searching online, but so far can’t find solutions that work. I’d appreciate any guidance offered. Thank you in advance.

    Viewing 4 reply threads
    Author
    Replies
    • #2395723

      zat_so,

      This should do what you want:

      $ous = ‘OU=OU1,DC=xxxx,DC=xxxx’,’OU=OU2,DC=xxxx,DC=xxxx’
      $Users = $ous| ForEach { Get-ADUser -Filter * -SearchBase $_ | Select SAMAccountName}
      $Users = $Users | sort 
      $Users >> C:\Users\Public\Documents\usernames.txt
      

      I don’t have access to a server so I wrote a little test program for the last half of the code:

      $users = "Minnie","Pluto","Mickey","Donald"
      $users = $users | Sort
      $users >> G:\Test\UserNames.txt
      <#
             Sample Output File Contents:
      
      Donald
      Mickey
      Minnie
      Pluto
      #>
      

      HTH

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      1 user thanked author for this post.
      • #2395729

        RG,

        Thank you very much for the reply. Unfortunately, the output file I got contained just one blank line. It would appear that $Users is not being populated in Line 2. Any thoughts?

        Thanks again.

    • #2395753

      zat_so,

      Next step is to simplify and see if we can find the problem. Try this:

      
      
      $ous = ‘OU=OU1,DC=xxxx,DC=xxxx’
      $Users = (Get-ADUser -Filter * -SearchBase $ous) | Select SAMAccountName
      $Users = $Users | sort 
      $Users = C:\Users\Public\Documents\usernames.txt
      

      If that works try it again with the other term and see what results you get.

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #2395762

        RG,

        I got the same (blank) results with each OU. What should I try next?

    • #2395789

      zat_so,

      Ok, let’s give this a try:

      $Users = Get-ADUser
      If ($Users.count -gt 0) {
        $Users[0] | gm >> d:\path\ADUser-Members.txt
      }
      

      Post the file here. Remember to replace the d:\path with actual information for your system.

      Note: you can use a text editor to remove any sensitive information from the file before posting.

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #2395907

        RG,

        Here’s what happened when I ran that:

        PS C:\WINDOWS\system32> $Users = Get-ADUser
        If ($Users.count -gt 0) {
        $Users[0] | gm >> d:\ADUser-Members.txt
        }
        cmdlet Get-ADUser at command pipeline position 1
        Supply values for the following parameters:
        (Type !? for Help.)
        Filter: !?
        A filter, such as ‘samAccountName -like “admin*”‘, which is used to search the directory for matching users.
        Filter: SamAccountName -like “*”

        I have attached the output file.

    • #2395926

      zat_so,

      Sorry, but I think I’m out of my depth here. Without access to a Domain Server that I can run the Get-ADUser cmdlet against I can’t really debug this. If nobody else here chimes in with useful information I’d suggest you go to StackOverFlow.com and post in the PowerShell forum.

       

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #2395931

        RG,

        I completely understand, and I very much appreciate you taking time to try. As I said in my OP, I can work around it, but it seemed like a problem with an easy fix.

        Thanks again.

    • #2395932

      According to Get-ADUser (ActiveDirectory) | Microsoft Docs you need the Filter parameter to get more than one user at a time. Try using -Filter * parameter on Get-Aduser

      --Joe

      • #2395934

        Joe,

        Thank you for your suggestion. The script I posted in the OP works fine for pulling the information. The issues I’m trying to resolve are that it includes

        SAMAccountName
        ————–

        as well as padding each line to (I assume) 80 characters with blanks. I can work around those two conditions, but it annoys me that I can’t get cleaner output.

    Viewing 4 reply threads
    Reply To: Powershell script needs minor tweaks: Remove header/extra spaces in output file

    You can use BBCodes to format your content.
    Your account can't use Advanced BBCodes, they will be stripped before saving.