33

I'm new to PowerShell and have a script which loops through Active Directory searching for certain computers. I get several variables and then run functions to check things like WMI and registry settings.

In the console, my script runs great and simple Write-Host command prints the data on the screen as I want. I know about Export-Csv when using the pipeline...but I'm not looking to print from the pipeline.

I want to write the variables to a text file, continue the loop, and check the next computer in AD...output the next iteration of the same variables on the next line. Here is my Write-Host:

Write-Host ($computer)","($Speed)","($Regcheck)","($OU) 

Output file:

$computer,$Speed,$Regcheck | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200 

It gives me the data, but each variable is on its own line. Why? I'd like all the variables on one line with comma separation. Is there a simple way to do this akin to VB writeline? My PowerShell version appears to be 2.0.

6 Answers 6

40

Use this:

"$computer, $Speed, $Regcheck" | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200 
Sign up to request clarification or add additional context in comments.

1 Comment

This should be the accepted answer. It's clean and simple. I bet most people miss the fact that the double quote wrapping the variables is what does the trick.
33

I usually construct custom objects in these loops, and then add these objects to an array that I can easily manipulate, sort, export to CSV, etc.:

# Construct an out-array to use for data export $OutArray = @() # The computer loop you already have foreach ($server in $serverlist) { # Construct an object $myobj = "" | Select "computer", "Speed", "Regcheck" # Fill the object $myobj.computer = $computer $myobj.speed = $speed $myobj.regcheck = $regcheck # Add the object to the out-array $outarray += $myobj # Wipe the object just to be sure $myobj = $null } # After the loop, export the array to CSV $outarray | export-csv "somefile.csv" 

4 Comments

When I run this, $computer isn't showing with a machine name? It is showing in the CSV as "System.DirectoryServices.PropertyValueCollection"
Okay, I have no idea where your $computer variable is generated, but the class you're referring to has a ToString method, so you might be able to do this: $myobj.computer = $computer.ToString(). If that doesnt work I will need to know what exactly you put in your $computer variable.
This worked! Thanks. FYI...the computer variable is pulled direct from AD technet.microsoft.com/en-us/library/ff730959.aspx
Great! I guess out-file does an implicit string conversion whereas export-csv does not. Anyhow, this technique will allow you to slice and dice data and is much more "object-oriented" so AFAIK it's the "right" way to do things (at least in my head:-) )
9

You can concatenate an array of values together using PowerShell's `-join' operator. Here is an example:

$FilePath = '{0}\temp\scripts\pshell\dump.txt' -f $env:SystemDrive; $Computer = 'pc1'; $Speed = 9001; $RegCheck = $true; $Computer,$Speed,$RegCheck -join ',' | Out-File -FilePath $FilePath -Append -Width 200; 

Output

pc1,9001,True 

1 Comment

This worked for me, but I had to use double quotes around the join string. ","
2

$computer,$Speed,$Regcheck will create an array, and run out-file ones per variable = they get seperate lines. If you construct a single string using the variables first, it will show up a single line. Like this:

"$computer,$Speed,$Regcheck" | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200 

Comments

1

The simple solution is to avoid creating an array before piping to Out-File. Rule #1 of PowerShell is that the comma is a special delimiter, and the default behavior is to create an array. Concatenation is done like this.

$computer + "," + $Speed + "," + $Regcheck | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200 

This creates an array of three items.

$computer,$Speed,$Regcheck FYKJ 100 YES 

vs. concatenation of three items separated by commas.

$computer + "," + $Speed + "," + $Regcheck FYKJ,100,YES 

Comments

1

I was lead here in my Google searching. In a show of good faith I have included what I pieced together from parts of this code and other code I've gathered along the way.

# This script is useful if you have attributes or properties that span across several commandlets # and you wish to export a certain data set but all of the properties you wish to export are not # included in only one commandlet so you must use more than one to export the data set you want # # Created: Joshua Biddle 08/24/2017 # Edited: Joshua Biddle 08/24/2017 # $A = Get-ADGroupMember "YourGroupName" # Construct an out-array to use for data export $Results = @() foreach ($B in $A) {	# Construct an object $myobj = Get-ADuser $B.samAccountName -Properties ScriptPath,Office	# Fill the object	$Properties = @{	samAccountName = $myobj.samAccountName	Name = $myobj.Name	Office = $myobj.Office	ScriptPath = $myobj.ScriptPath	} # Add the object to the out-array $Results += New-Object psobject -Property $Properties	# Wipe the object just to be sure $myobj = $null } # After the loop, export the array to CSV $Results | Select "samAccountName", "Name", "Office", "ScriptPath" | Export-CSV "C:\Temp\YourData.csv"

Cheers

1 Comment

The problem is this output is as a CSV and the request was as a flat file, not a CSV.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.