13

I am creating a script and want to both use Write-Host and Write-Output As I work I want a backup of information I pull from AD to also become attached to a .txt file. This is more of a backup in case I miss a piece of information and need to go back and recreate a ticket. Anyways I have a sample of my script, form what I can tell it should be working. If someone with a bit more experience can take a look or point me in the right direction I would appreciate it. If I need to add any more of the script I can provide this. Thanks in Advance.

Import-Module activedirectory
$object = Get-ADUser $sid -Properties * | Select-Object EmailAddress
Write-Host Email: $object.EmailAddress
Write-Output ("Email: $object.EmailAddress") >> C:\psoutput\psoutput.txt -Append

This will create the .txt file of course but is also add other information such as:

Email: @{GivenName=myfirstname; Surname=mylastname; SamAccountName=myid; DisplayName=lastname, firstname - Contingent Worker; City=; [email protected]; EmployeeID=; Enabled=True; OfficePhone=; MobilePhone=(555) 555-5555; LockedOut=False; LockOutTime=0; AccountExpirationDate=05/09/2020 00:00:00; PasswordExpired=False; PasswordLastSet=12/03/2019 12:16:37}.EmailAddress
-Append

I am looking to have the output like the following...

name: username
email: user email address
phone: user phone number
etc...

All general information from Active Directory

Thanks again for the suggestions

0

4 Answers 4

7

Don't use write-output. Use (Get-ADUser $sid -properties mail).mail. Like this:

Add-Content -Path "FilePath" -Value "Email: $((Get-ADUser $sid -properties mail).mail)"
Sign up to request clarification or add additional context in comments.

Comments

6

Write-Output ("Email: $object.EmailAddress")

As an aside: No need for (...) here.

This doesn't do what you expect it to: it stringifies $object as a whole and then appends .EmailAddress verbatim; in order to embed an expression, such as accessing a property inside "..." (an expandable string), you need $(...), the subexpression operator.

Write-Output "Email: $($object.EmailAddress)" >> C:\psoutput\psoutput.txt

See this answer for an overview of the syntax in PowerShell expandable strings.

Or, more simply, using PowerShell's implicit output behavior (explicit use of Write-Output is rarely necessary):

"Email: $($object.EmailAddress)" >> C:\psoutput\psoutput.txt

>> C:\psoutput\psoutput.txt -Append

>> is effectively an alias for Out-File -Append (just like > is for just Out-File), so not only is there no need for -Append, it isn't interpreted by >>, which accepts only the filename operand.
Instead, -Append was interpreted by Write-Output, which is why it ended up literally in your output file.
Perhaps surprisingly, while a redirection such as >> C:\psoutput\psoutput.txt is typically placed last on the command line, that is not a syntactic requirement: other arguments may follow.


I am looking to have the output like the following..

It sounds like you want formatting as provided by the Format-List cmdlet:

$object | Format-List >> C:\psoutput\psoutput.txt

Note that > / >> / Out-File apply the default string formatting, via PowerShell's for-display output-formatting system, i.e. the same representation that would by default display in the console.

By using an explicit Format-* cmdlet, you can control that formatting, but note two things about Out-File in general:

  • Given that you're outputting for-display formats, the resulting file may not be suitable for subsequent programmatic processing.

  • To prevent truncation of values, you may have to do one or more of the following: pass a -Width argument to Out-File, control the enumeration length of nested properties via $FormatEnumerationLimit, and, in the case of (possibly implicitly used) Format-Table, pass -AutoSize.

Comments

3

You don't really need to use Write-Output at all. Try this to just get your string to your file:

("Email: " + $object.EmailAddress) >> C:\psoutput\psoutput.txt

You don't need to specify append because '>>' already does that for you

Comments

0

Example with tee-object, which writes both to the screen and a file

$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8' # default utf16 in ps5.1

get-aduser js2010 -property EmailAddress | % emailaddress | tee-object log -Append

[email protected]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.