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:
- The output has a header.
- 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.