?
Solved

Powershell: Export variables to csv

Posted on 2014-02-20
3
Medium Priority
?
2,450 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 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
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 …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

777 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