Hi, im currently working on making list of users and their phone numbers in excel, data is taken from Active Directory so there is no problem, but i approached some problems with generating description for my document, i need to put logo, document name, and description on top of the document.
Import-Module ImportExcel
#Variable with route of file saved.
$ExportFolder = "C:\phonebook.xlsx"
#Counts ammount of records
$recordsCount = (Get-ADUser -Filter {(Company -like '*Company*') -and (Enabled -eq $true)} -Properties Description, telephoneNumber |
Select-Object Name, Description, @{Name='telephoneNumber';Expression={$_.telephoneNumber + "`t"}} |
Where-Object { $_.telephoneNumber -ne $null -and $_.telephoneNumber.Length -gt 6 }).Count
#Uses ammount of records to cut it in half to create 2 tables later it gives back only intigers.
$columnsper2 = [Math]::Ceiling($recordsCount/2)
#If file exist already it deletes, it helps with errors that append while overwriting file.
if (Test-Path $ExportFolder) {
Remove-Item $ExportFolder
}
#Gets users for first column
Get-ADUser -Filter {(Company -like '*Company*') -and (Enabled -eq $true)} -Properties Description, telephoneNumber |
Select-Object Name, Description, @{Name='telephoneNumber';Expression={$_.telephoneNumber + "`t"}} |
Where-Object { $_.telephoneNumber -ne $null -and $_.telephoneNumber.Length -gt 6 } |
Sort-Object Name |
Select-Object -First $columnsper2|
Export-Excel -Path "$ExportFolder" -FreezeTopRow -BoldTopRow -StartRow 2 -StartColumn 1 -AutoSize -TableStyle Medium6
#Gets users for second column
Get-ADUser -Filter {(Company -like '*Company*') -and (Enabled -eq $true)} -Properties Description, telephoneNumber |
Select-Object Name, Description, @{Name='telephoneNumber';Expression={$_.telephoneNumber + "`t"}} |
Where-Object { $_.telephoneNumber -ne $null -and $_.telephoneNumber.Length -gt 6 } |
Sort-Object Name |
Select-Object -Skip $columnsper2|
Export-Excel -Path "$ExportFolder" -FreezeTopRow -BoldTopRow -StartRow 2 -StartColumn 5 -AutoSize -TableStyle Medium6
This is code that i made so far, my goal is to put Company name on first row, some informations and logo.
I would really appreciate also if someone tell me if im doing something wrong with current code, its my first touch with powershell, as well my first IT job so im trying my best but i probably could do some silly mistakes