Solved

Powershell: Export variables to csv

Posted on 2014-02-20
3
2,314 Views
Last Modified: 2014-02-20
Hi,

I have a powershell script that gives me some output on screen.
I would like to have this exported into a csv file.
It must be quite simple I guess, but with my lack of Powershell knowledge, i can not get it done.
The script displays:

doe, john migrated
smith, john no longer available
jackson, jack not migrated

Import-Module ActiveDirectory
$users =Get-ADUser -filter * | select samaccountname | foreach {
$Username = $_.sAMAccountname
$ADuser = Get-ADUser -identity $Username –properties *
$Name = $AdUser.CN
$UserOU1 = $ADuser.CanonicalName.ToString().Split('/')[3]
$UserOU2 = $ADuser.CanonicalName.ToString().Split('/')[1]
   if ($userOU1 -match "employees") {
            $status = "migrated"
	    	write-host $Name $status -ForegroundColor green
        } 
        elseif ($userOU2 -match "out of service") {
            $status = "No longer available"
                write-host $Name $status -ForegroundColor white
        }
        else {
            $status = "not migrated"
                write-host $Name $status -ForegroundColor red
        }


}

Open in new window

0
Comment
Question by:Loyall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Expert Comment

by:Stelian Stan
ID: 39873260
You should be able to add "Export-Csv C:\test.csv" at the end of your script as line 23.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39873267
Try creating a custom object and then use Export-csv to export the details to csv file..
Import-Module ActiveDirectory
$users =Get-ADUser -filter * | select samaccountname | foreach {
$Username = $_.sAMAccountname
$ADuser = Get-ADUser -identity $Username –properties *
$Name = $AdUser.CN
$UserOU1 = $ADuser.CanonicalName.ToString().Split('/')[3]
$UserOU2 = $ADuser.CanonicalName.ToString().Split('/')[1]
   if ($userOU1 -match "employees") {
            $status = "migrated"
	    	write-host $Name $status -ForegroundColor green
				} 
        elseif ($userOU2 -match "out of service") {
            $status = "No longer available"
                write-host $Name $status -ForegroundColor white
        }
        else {
            $status = "not migrated"
                write-host $Name $status -ForegroundColor red
        }
"" | Select @{N="Name";E={$Name}},@{N="Status";E={$status}}
} | Export-Csv C:\report.csv -nti

Open in new window

0
 
LVL 2

Author Closing Comment

by:Loyall
ID: 39873278
Subsun, thanks a lot ! Exactly what i needed !!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question