Pobranie danych konta użytkownika z Active Directory do Excel’a

erroractionpreference = “SilentlyContinue” 
$a = New-Object -comobject Excel.Application 
$a.visible = $True

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = “Osoba” 
$c.Cells.Item(1,2) = “Description” 
$c.Cells.Item(1,3) = “Title” 
$c.Cells.Item(1,4) = “MemberOf” 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 
$d.EntireColumn.AutoFit()

$intRow = 2

$persons=Get-Content d:\persons.txt

foreach ($person in $persons) 
{
$grupy= Get-ADUser -Identity $person -Property * |Select -Property SamAccountName,Name,description,title,
 	@{name='MemberOf';
 	expression={ ($_.MemberOf | ForEach { ($_ -split ',')[0] -replace 'CN=',''}) -join ','}}
if ($grupy.Name -eq $null){
$c.Cells.Item($intRow, 1) ="brak w AD"
$c.Cells.Item($intRow, 2) ="brak w AD"
$c.Cells.Item($intRow, 3) ="brak w AD"
$c.Cells.Item($intRow, 4) ="brak w AD"
$intRow = $intRow + 1
}

else{
$c.Cells.Item($intRow, 1) = $grupy.SamAccountName
$c.Cells.Item($intRow, 2) =$grupy.Title
$c.Cells.Item($intRow, 3) =$grupy.Description
$c.Cells.Item($intRow, 4) =$grupy.MemberOf}
$intRow = $intRow + 1 
} 

$d.EntireColumn.AutoFit()