I have created a Power Shell Script for Office 365 groups to export certain data we are looking for.
the part I'm stuck on is when I use Write-Host that creates a variable, this displays correctly, so I know it's getting the data:
Write-Host "Newest Inbox Traffic:" $CheckDate = (Get-MailboxFolderStatistics -Identity $Group.Alias -IncludeOldestAndNewestItems -FolderScope Inbox).NewestItemReceivedDate -ForegroundColor White
Now when I create the export to Excel, I'm selecting fields in the order I want them to appear in the table but how do I add the variable:
Get-UnifiedGroup -Identity $Group.Id |
Select DisplayName, AccessType, WhenCreated, WhenChanged, $CheckDate, GroupMemberCount, GroupExternalMemberCount, IsMembershipDynamic |
Export-CSV $CSVPath2 -NoTypeInformation -Append
Ideally, I would also like to label the variable in excel so it doesn't show in the title column as $CheckDate
Here is full code, this might explain what I'm trying to achieve, the display on screen part works perfectly, I just need that to transfer to Excel:
$CSVPath = "C:\AllGroupNoMembers.csv"
$CSVPath2 = "C:\AllGroupNoOwner.csv"
$TodaysDate = (Get-Date)
#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False
#Remove the CSV file if exists
If(Test-Path $CSVPath) { Remove-Item $CSVPath}
If(Test-Path $CSVPath) { Remove-Item $CSVPath2}
#Find M365 groups with no owners
$o365GroupsNO=Get-UnifiedGroup | Where-Object {-Not $_.ManagedBy}
ForEach ($Group in $o365GroupsNO)
{
$CheckDate=(Get-MailboxFolderStatistics -Identity $Group.DisplayName -IncludeOldestAndNewestItems -FolderScope Inbox).NewestItemReceivedDate
$Members=(Get-UnifiedGroupLinks -Identity $Group.DisplayName -LinkType Members) -join '; '
#Display on screen
Write-Host "Group Name:" $Group.DisplayName -ForegroundColor Green
Write-Host "Owner:" $Group.ManagedByDetails -ForegroundColor Red
Write-Host "Name:" $Group.DisplayName -ForegroundColor Yellow
Write-Host "Access:" $Group.AccessType -ForegroundColor Yellow
Write-Host "Created:" $Group.WhenCreated -ForegroundColor White
Write-Host "Changed:" $Group.WhenChanged -ForegroundColor White
Write-Host "Newest Inbox Traffic:" $CheckDate -ForegroundColor White
Write-Host "Group Member Count:" $Group.GroupMemberCount -ForegroundColor Gray
Write-Host "Group Members:" $Members -ForegroundColor Gray
Write-Host "Ext Group Member Count:" $Group.GroupExternalMemberCount -ForegroundColor Gray
Write-Host "Dynamic Membership:" $Group.IsMembershipDynamic -ForegroundColor Gray
Write-Host "SharePoint URL:" $Group.SharePointSiteUrl -ForegroundColor Gray
Write-Host "SharePoint Document URL:" $Group.SharePointDocumentsUrl -ForegroundColor Gray
Write-Host "Notes:" $Group.Notes -ForegroundColor Gray
}
#Export to CSV
Get-UnifiedGroup -Identity $Group.Id | Select DisplayName, AccessType, WhenCreated, WhenChanged, $CheckDate, GroupOwnerCount, GroupMemberCount, $Members, GroupExternalMemberCount, IsMembershipDynamic, SharePointSiteUrl, SharePointDocumentsUrl, Notes| Export-CSV $CSVPath2 -NoTypeInformation -Append -Force
#Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$False
Write-Host ... $CheckDate = ...isn't assigning a value to a variable called$CheckDate- it's printing the contents of a variable called$CheckDate(which is unassigned unless you've previously given it a value). Your code is equivalent toWrite-Host @( "Newest Inbox Traffic:", $CheckDate, "=", (Get-MailboxFolderStatistics ...).NewestItemReceivedDate ) -ForegroundColor White.. Try assigning the value to the$CheckDatevariable on a separate line and then justWrite-Host "Newest Inbox Traffic: $CheckDate" -ForegroundColor White.