I downloaded Excel report but I need to resize some of the columns to different width using PowerShell so I'm just wondering how can I achieve that. Any help or suggestion will be appreciated.
For Example, I want to modify User, Date & Time, Item column to width size 30, Activity Column to width size 50 and some other column to width size 30 so on...
function Modify-Columns {
$params = @{
AutoSize = $true
# TableName = 'exampleTable'
TableStyle = 'Light9' # => Here you can chosse the Style you like the most
BoldTopRow = $true
WorksheetName = "Audit Log"
PassThru = $true
Path = "C:\AuditLogSearch\$((Get-Date).AddDays(-7).ToString('yyyy-MM-dd')) _ $(Get-Date -Format "yyyy-MM-dd") Audit-Log-Records.xlsx" # => Define where to save it here!
}
Write-Host "Start Modifiying Column and Row using AD DisplayName and Excel Files"
$modifiedFile = Import-Csv "C:\AuditLogSearch\Modified-Audit-Log-Records.csv"
$actionReference = Import-Csv "C:\AuditLogSearch\Reference\eDiscovery_Activities_List.csv"
$result = foreach ($u in $modifiedFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
New-Object PsObject -Property @{
User = $u.User
# "Search Criteria" = $u."Search Criteria"
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Search Conditions" = $u."Search Conditions"
"SharePoint Sites" = $u."SharePoint Sites"
"Exchange Public Folders" = $u."Exchange Public Folders"
"Exchange Mailboxes" = $u."Exchange Mailboxes"
"Case Name" = $u."Case Name"
"Search Criteria" = $u."Search Criteria"
"Item" = $u."Item"
"Activity" = if (($actionReference | where-object { $_.Name -eq $u."Activity" }).Value -ne $null) { ($actionReference | where-object { $_.Name -eq $u."Activity" }).Value }
else { $u."Activity" }
} | Select-object -Property User, "Date & Time", "Case Name", "Item", "Activity" , "SharePoint Sites", "Exchange Mailboxes", "Exchange Public Folders" , "Search Criteria", "Result Status"
}
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
Close-ExcelPackage $xlsx
Write-Host "Finished Modifiying Column and Row using AD DisplayName and Excel Files"
}
CodePudding user response:
As you may have seen, the -AutoSize
is not very exact, it tends to leave more width than needed. In case you need to set a hardcoded value to one of the columns you can use:
$xlsx.Workbook.Worksheets['SheetName'].Column(n).width = newVal
NOTE: This method requires the use of -PassThru
.
Here is a minimal reproducible example:
$result = [pscustomobject]@{
Col1 = 'Test'
Col2 = 'Test'
Col3 = [datetime]::Now
}
$params = @{
AutoSize = $true
TableStyle = 'Light9'
BoldTopRow = $true
WorksheetName = "Audit Log"
PassThru = $true
Path = './test.xlsx'
}
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.WorksheetName]
# This is how you can get the number of Columns, Index starts from 1 not 0.
$ws.Dimension.Columns # => 3
$ws.Column(1).Width # => Col1 current Width is 9.140625
$ws.Column(1).Width = 5 # => Updated to 5
$ws.View.ShowGridLines = $false # => Hide the GridLines
Close-ExcelPackage $xlsx