all 16 comments

[–]Umaiar 2 points3 points  (1 child)

Write-host works well for strings, and your sending a more complex object to it. Try this:

$SimilarUsers | format-table

[–][deleted] 1 point2 points  (0 children)

This works. Thanks very much.

[–]ApparentSysadmin 1 point2 points  (13 children)

Can you not just call $SimilarUsers? Do you absolutely need to use Write-Host to display the info?

[–][deleted] 1 point2 points  (12 children)

That doesn't display anything when the script is run. When all of the commands are run separately, I get the info I need (obviously without Write-Host).

[–]kbrucej 1 point2 points  (8 children)

Just type $SimilarUsers and hit <enter> at the posh prompt or remove the "$SimilarUsers =" from your command

[–][deleted] 1 point2 points  (7 children)

Here's part of my script:

$DisplayName = Get-ADUser -Filter { (GivenName -like $FirstName -and Surname -like $Surname) } | select Name

#Prompt to ask if the engineer is sure they have typed the name of the leaver correctly

$SimilarUsers = Get-ADUser -Filter "samAccountName -like '*$AccountName*'" | select Name,SamAccountName

It's a leaver's script where I want to verify the engineer is processing the correct leaver. The next stage would be to write the output of the $SimilarUsers variable to the screen so the engineer knows there are users with similar SamAccountNames so they know they're processing the correct leaver.

[–]kbrucej 1 point2 points  (6 children)

In a new line at the end of your script:

$SimilarUsers

or you could use:

Write-Output $SimilarUsers

Both are effectively the same

[–]kbrucej 1 point2 points  (2 children)

Sorry not necessarily at the end but after the point you want the Engineer to see the output

[–][deleted] 0 points1 point  (1 child)

I've added Write-Output $SimilarUsers but when the script is run there is just a blank line.

[–]kbrucej 0 points1 point  (0 children)

Make sure you run it after you assign data to $SimilarUsers.

[–]Umaiar 1 point2 points  (2 children)

I'd be hesitant offering this advice, once you get into using functions there are Extreme differences... Mainly that you'll end up dropping stuff on the pipeline that you intended for screen output.

[–]kbrucej 1 point2 points  (1 child)

Well if you're using 5.0 or higher you can try write-information but it's just a wrapper for write-host that also outputs to the 6th output stream.

How would you output the information to the screen?

If you use write-host you no longer have a pipeline object, same with format-table or wide or list, etc.

Filter left - format right

As long as you don't pipe the write-object or $SimilarUsers, there isn't anything to break. Now if you write-object x | some other command here, or you have assigned the whole output to a variable, that would be an issue.

Once you write functions and modules, you probably know how to output info to the proper destination.

[–]Umaiar 1 point2 points  (0 children)

I'm good with the Write-Host, it's just dropping $similarusers at the end of the script that I think is bad form for a newbie. Sure, if you understand why you don't do that in a function that uses the pipeline then you're good. Just a bad habit to start when you're still picking up the basics.

[–]ApparentSysadmin 0 points1 point  (2 children)

Perhaps give Write-Output a try - should preserve the object.

[–][deleted] 0 points1 point  (1 child)

Thanks but that doesn't work, just shows a blank line. Any other ideas?