Solved

Powershell: Export variables to csv

Posted on 2014-02-20
3
2,216 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
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…
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

738 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