Wchristner
Wchristner

Reputation: 187

powershell Write-Output with >> to a file.txt -Append

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

Upvotes: 12

Views: 49576

Answers (3)

mklement0
mklement0

Reputation: 439238

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 (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, 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:

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

  • To prevent truncation of values, you may have to pass a -Width argument to Out-File, control the enumeration length of nested properties with $FormatEnumerationLimit, and, in the case of Format-Table, specify -AutoSize.

Upvotes: 4

wasif
wasif

Reputation: 15488

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)"

Upvotes: 6

Shamus Berube
Shamus Berube

Reputation: 486

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

Upvotes: 2

Related Questions