Removing White Spaces

Hello all, i have a pretty simple script to return usernames based off of full names that are being read in by a text file. All is well regarding the script, however the output has some white space that im having difficulty getting rid of. Here is the script....

$users = get-content c:\users\user\desktop\names.txt

$results = foreach ($item in $Users) {get-aduser -filter {name -eq $item} | select-object samaccountname}

$results | out-file c:\users\user\desktop\results.txt

i have attached a screenshot of what the output actually looks like in a text file, you can see when i do a "select all" that theres a ton of white space after each result.

May 22nd, 2015 11:09am

At first glance, you may want to use Select-Object's -ExpandProperty parameter: ... | Select-Object -ExpandProperty SamAccountName

Edit: Alexander's option will work as well, providing you're using PowerShell 3.0 or greater. Dotted notation does the same thing as the -ExpandProperty parameter -- isolates only the property's value and rids you of the column header, underline, and excess spaces.


Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 11:30am

$users = get-content c:\users\user\desktop\names.txt

$results = foreach ($item in $Users) {(get-aduser -filter {name -eq $item}).samaccountname}

$results | out-file c:\users\user\desktop\results.txt

Just a tiny little change :)

May 22nd, 2015 11:31am

i knew it would be something simple, i tried to keep this script as light as possible. That did the trick, took out litrerally every white space. Great add there sir, appreciate it!!
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 11:33am

Is there any way i can tell powershell to ignore blank lines in the text file its reading from? For example if you have a text file that reads names from top to bottom, but has a line break between each one.
May 22nd, 2015 1:42pm

If your file contents look like this:

one

two

three

four

Then you can remove blank lines like this:

Get-Content -Path C:\file.txt | Where-Object {$_ -ne ''}

And, get this, in return:

one
two
three
four

Edit: Typo



Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 1:49pm

At first glance, you may want to use Select-Object's -ExpandProperty parameter: ... | Select-Object -ExpandProperty SamAccountName

Edit: Alexander's option will work as well, providing you're using PowerShell 3.0 or greater. Dotted notation does the same thing as the -ExpandProperty parameter -- isolates only the property's value and rids you of the column header, underline, and excess spaces.


May 22nd, 2015 3:22pm

If your file contents look like this:

one

two

three

four

Then you can remove blank lines like this:

Get-Content -Path C:\file.txt | Where-Object {$_ -ne ''}

And, get this, in return:

one
two
three
four

Edit: Typo



Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 5:41pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics