Solved

Powershell: Export variables to csv

Posted on 2014-02-20
3
1,863 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Resolve DNS query failed errors for Exchange
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now