Cell formatting in excell via powershell code

NewDev1982 1 Reputation point
2021-11-03T12:48:28.013+00:00

I have the following Powershell code:

$users = Get-ADUser -Filter * -Properties * -SearchBase 'OU=User Accounts,DC=itiz-ct-cus3,DC=local' -SearchScope 2 | Sort-Object displayname

foreach ($user in $users)
{
$name = $user.displayname
$phone = $user.ipphone
$tele = $user.telephonenumber
if ($name -like "ctu")
{

}
else {
    "$name,$phone,$tele" | Out-File -FilePath '\\corp\fileshare\IT\Reports\test.csv' -Encoding ascii -Append 
    Write-Host $name $phone
}

}

I am trying to have the output format the cell size in excel. I tried using the Format-table -Autosize, however that was unsuccessful.
Any advice would be greatly appreciated

Microsoft 365 and Office | Development | Other
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 48,036 Reputation points
    2021-11-03T14:56:04.493+00:00

    You'll have to use the Excel,Application COMObject or the ImportExcell PowerShell module to do that.

    CSV file are text-only and contain no "formatting" at all.

    You'll find plenty of examples on manipulating cell formatting using the Excel.Application by searching the for things like "powershell excel com object", "Powershell Excel Automation", etc. The problem with using that is that it can only be used on a machine that has Excel installed on it (which eliminates servers).

    The ImportExcel module doesn't depend on the presence of Excel and is, to most people's way of thinking, easier to use.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.