Link to home
Start Free TrialLog in
Avatar of Loyall
LoyallFlag for Netherlands

asked on

Powershell: Export variables to csv

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

Avatar of Stelian Stan
Stelian Stan
Flag of Canada image

You should be able to add "Export-Csv C:\test.csv" at the end of your script as line 23.
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Loyall

ASKER

Subsun, thanks a lot ! Exactly what i needed !!