Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell - Export to multiple sheets in an Excelfile

$
0
0

Hello,

Ik hope somebody can help me. Ik have a Powershell script and i use this to export a couple OU's in the Active Directory.
This exports go each to there own CSV file (as backup). I realy would like the exports go to one Excel file, each in different sheets.

So for example if i have 3 OU's: Employees, OU: Functional Mailboxes and OU: Disabled Accounts.

I want the OU's have there own sheet in an .XLSX-file so i can easely make an report of the accounts.

Below is the script that i use to export one OU and make an CSV file as backup and export to XLSX.

I miss the import part for multiple CSV's:

clear-host

import-module ActiveDirectory

$Employees="OU=Employees,OU=Accounts,OU=LocationA,DC=DOMAIN,DC=COM"

$workbook = $excel.workbooks.add()
$sheet = $workbook.workbooks.Item(1)
$sheet.cells.item(1,1) = "Test"

$Users = Get-ADUser -Filter * -SearchBase $Employees -Properties * | Select-Object -Property Name,Description | 
Sort-Object -Property Name | Where-Object {$_.Name -like "*" -and $_.Enabled -ne $TRUE }| 
Export-Csv -Delimiter “,” -Path "C:\export_users.csv” -NoTypeInformation

$excel = new-object -comobject excel.application
$excel.visible = $true
$workbook = $excel.workbooks.add(“C:\export_users.csv”)
$workbook.workSheets.Item(3).delete()
$workbook.workSheets.Item(2).delete()
$workbook.WorkSheets.Item(1).Name = "Users"
$sheet = $workbook.WorkSheets.Item("Users")
$x = 2
$sheet.cells.item(1,1) = "Name"
$sheet.cells.item(1,2) = "Description"

foreach($ADUser in $Users)
{
 $sheet.cells.item($x, 1) = $Users.name
 $sheet.cells.item($x,2) = $Users.workingSetSize
 $x++
} 

$range = $sheet.usedRange
$range.EntireColumn.AutoFit() | out-null

$table=$workbook.ActiveSheet.ListObjects.add( 1,$workbook.ActiveSheet.UsedRange,0,1)
$workbook.ActiveSheet.UsedRange.EntireColumn.AutoFit()
$excel.DisplayAlerts=$False
$workbook.SaveAs("C:\Export.xlsx",51)

On this pont the sheets 2 and 3 will be deleted but i would realy like the sheets filt with de other OU's

Ik hope someone can help me with this.

Sorry for crappy English.

Greets,

Hendrik.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>