How to get Email Addresses from All Folders of my mailbox through PowerShell
Thanks to following URL, please find code for InBox
https://stackoverflow.com/questions/44068261/get-email-items-from-mailboxes-through-powershell
I change following line to include Email Address
Select-Object -Property Subject, ReceivedTime, Importance, SenderName, SenderEmailAddress
but need help to get All Folders
Function Get-OutlookInBox
{
<#
.Synopsis
This function returns InBox items from default Outlook profile
.Description
This function returns InBox items from default Outlook profile. It
uses the Outlook interop assembly to use the olFolderInBox enumeration.
It creates a custom object consisting of Subject, ReceivedTime, Importance,
SenderName for each InBox item.
*** Important *** depending on the size of your InBox items this function
may take several minutes to gather your InBox items. If you anticipate
doing multiple analysis of the data, you should consider storing the
results into a variable, and using that.
.Example
Get-OutlookInbox |
where { $_.ReceivedTime -gt [datetime]"5/5/11" -AND $_.ReceivedTime -lt `
[datetime]"5/10/11" } | sort importance
Displays Subject, ReceivedTime, Importance, SenderName for all InBox items that
are in InBox between 5/5/11 and 5/10/11 and sorts by importance of the email.
.Example
Get-OutlookInbox | Group-Object -Property SenderName | sort-Object Count
Displays Count, SenderName and grouping information for all InBox items. The most
frequently used contacts appear at bottom of list.
.Example
$InBox = Get-OutlookInbox
Stores Outlook InBox items into the $InBox variable for further
"offline" processing.
.Example
($InBox | Measure-Object).count
Displays the number of messages in InBox Items
.Example
$InBox | where { $_.subject -match '2011 Scripting Games' } |
sort ReceivedTime -Descending | select subject, ReceivedTime -last 5
Uses $InBox variable (previously created) and searches subject field
for the string '2011 Scripting Games' it then sorts by the date InBox.
This sort is descending which puts the oldest messages at bottom of list.
The Select-Object cmdlet is then used to choose only the subject and ReceivedTime
properties and then only the last five messages are displayed. These last
five messages are the five oldest messages that meet the string.
.Notes
NAME: Get-OutlookInbox
AUTHOR: ed wilson, msft
LASTEDIT: 05/13/2011 08:36:42
KEYWORDS: Microsoft Outlook, Office
HSG: HSG-05-26-2011
.Link
Http://www.ScriptingGuys.com/blog
#Requires -Version 2.0
#>
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$folder.items |
Select-Object -Property Subject, ReceivedTime, Importance, SenderName, SenderEmailAddress
} #end function Get-OutlookInbox
I got help on InBox but need help to get all folders
Syntax for Get-MailboxFolder follow This link for more abou
Get-MailboxFolder
[[-Identity] <MailboxFolderIdParameter>]
[-Recurse]
[-DomainController <Fqdn>]
[-MailFolderOnly]
[-ResultSize <Unlimited>]
[<CommonParameters>]
Try this code apply your fields.
$DomainSuffix = Read-Host "Type the domain name suffix that you look for"
# Creating folder structure and file names for the exported CSV files
$A20 = "C:\INFO\E-mail address\Exchange Online\E-mail address with Domain suffix - $Domainsuffix"
if (!(Test-Path -path $A20))
{New-Item $A20 -type directory}
# Exchange Online infrastructure
# Define the variable for the different type of Exchange Online recipients
$AllRecipients = Get-Recipient -ResultSize unlimited| Where {$_.EmailAddresses -like "*#$DomainSuffix"}
$SoftDeleted = Get-Mailbox -SoftDeletedMailbox | Where {$_.EmailAddresses -like "*#$DomainSuffix"}
$UnifiedGroups = Get-UnifiedGroup | Where {$_.EmailAddresses -like "*#$DomainSuffix"}
if ($SoftDeleted -eq $null)
{
write-host "There are no Soft Deleted Exchange Online mailboxes that have an E-mail address with the domain name suffix - $DomainSuffix"
}
else
{
$SoftDeleted | Select DisplayName,EmailAddresses,RecipientType,RecipientTypeDetails | Export-CSV $A20\"Exchange Online Soft Deleted mailboxes that their E-mail address include the domain name suffix $DomainSuffix.CSV" –NoTypeInformation -Encoding utf8
}
if ($UnifiedGroups -eq $null)
{
write-host "There are no Exchange Online unified groups that have an E-mail address with the domain name suffix - $DomainSuffix"
}
Else
{
$UnifiedGroups | Select DisplayName,EmailAddresses,RecipientType,RecipientTypeDetails | Export-CSV $A20\"Exchange Online unified groups that their E-mail address include the domain name suffix $DomainSuffix.CSV" –NoTypeInformation -Encoding utf8
}
if ($AllRecipients -eq $null)
{
write-host "There are no Exchange Online Recipients that have an E-mail address with the domain name suffix - $DomainSuffix"
}
Else
{
$AllRecipients | Select DisplayName,EmailAddresses,RecipientType,RecipientTypeDetails | Export-CSV $A20\"Exchange Online recipients that their E-mail address include the domain name suffix $DomainSuffix.CSV" –NoTypeInformation -Encoding utf8
}
for reference follow this Site
Related
I have used a script to get all users that have activesync enabled and list all of the connections. Many users have multiple entries where they have used a phone and upgraded, or re-enabled after a wipe.
I am looking to get rid of any entry above 30 days, only for users in a specific OU, or text file full of users.
I believe this code will work universally across the domain:
$DevicesToRemove = Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-30")}
$DevicesToRemove | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}
but I only want to do it for either an OU, or txt list.
I can create a .txt list of either the UPN, or the username, which may be easier than looking for all users in an OU. How would I modify that code (or altogether better code?) to remove 30 day+ activesync connections for that txt list?
Text file option would be preferred for a better target.
I think I self answered, so posting for others.
“==============================================================”
“Start Mailbox Retrieve”
“==============================================================”
$mbx = get-casmailbox -resultsize unlimited | where {$_.activesyncenabled -eq $true} ;
“==============================================================”
“End Mailbox Retrieve”
“==============================================================”
$mbx | foreach {
“Processing: “+$_.name
$name = $_.name;
$device = get-activesyncdevicestatistics -mailbox $_.identity | where {$_.LastSuccessSync -le (Get-Date).AddDays(“-30”)};
if($device){
{
”
Device: “+$dev.DeviceType
$csvRows += $dev
}
}
}
“==============================================================”
“Start CSV Write”
“==============================================================”
$csvRows | Export-Csv “c:\ps\staledevices.csv” -NoType
“==============================================================”
“End CSV Write”
“==============================================================”
From http://techtalklive.org/ttlblog/removing-stale-activesync-devices/
Then to remove:
Import-Csv c:\ps\staledevices.csv |foreach {remove-activesyncdevice -identity $_.guid -confirm:$false}
From http://techtalklive.org/ttlblog/removing-stale-activesync-devices/
A while ago I went through and starting using the script guys get-outlookInbox which worked great with Data mining Subjects, however, I'm now trying to basically do the same thing but by trying to do this with bodies.
Originally I was importing the custom script from the script guys: https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/26/use-powershell-to-data-mine-your-outlook-inbox/
Using the line
$inbox | Where-Object { $_.subject -match 'x'} | Where-Object { ($_.ReceivedTime -gt "06 11 2017") -and ($_.ReceivedTime -lt "10 10 2018") } | Group-Object -Property senderName -NoElement | Sort-Object count
I've tried changing $_.subject to $_.body however it seems that the original script doesn't pull body contents, i'm somewhat stuck here and was hoping someone might be able to point me in the right direction
The reason why it is not available is because the original script does not select the body. In order to get the body of the message you will need to change the original script to include the body.
Change:
Get-OutlookInbox.ps1
Change the line from:
$folder.items | Select-Object -Property Subject, ReceivedTime, Importance, SenderName
To:
$folder.items | Select-Object -Property Subject, ReceivedTime, Importance, SenderName, body
I need to retrieve email items from outlook from various mailboxes.
I found the below script that works fine for my inbox. But how do i define other mailbox name in the script.
I have many mailboxes and each hour i need to retrieve items from the mailboxes, hence looking out for automation which help save efforts.
Function Get-OutlookInBox
{
<#
.Synopsis
This function returns InBox items from default Outlook profile
.Description
This function returns InBox items from default Outlook profile. It
uses the Outlook interop assembly to use the olFolderInBox enumeration.
It creates a custom object consisting of Subject, ReceivedTime, Importance,
SenderName for each InBox item.
*** Important *** depending on the size of your InBox items this function
may take several minutes to gather your InBox items. If you anticipate
doing multiple analysis of the data, you should consider storing the
results into a variable, and using that.
.Example
Get-OutlookInbox |
where { $_.ReceivedTime -gt [datetime]"5/5/11" -AND $_.ReceivedTime -lt `
[datetime]"5/10/11" } | sort importance
Displays Subject, ReceivedTime, Importance, SenderName for all InBox items that
are in InBox between 5/5/11 and 5/10/11 and sorts by importance of the email.
.Example
Get-OutlookInbox | Group-Object -Property SenderName | sort-Object Count
Displays Count, SenderName and grouping information for all InBox items. The most
frequently used contacts appear at bottom of list.
.Example
$InBox = Get-OutlookInbox
Stores Outlook InBox items into the $InBox variable for further
"offline" processing.
.Example
($InBox | Measure-Object).count
Displays the number of messages in InBox Items
.Example
$InBox | where { $_.subject -match '2011 Scripting Games' } |
sort ReceivedTime -Descending | select subject, ReceivedTime -last 5
Uses $InBox variable (previously created) and searches subject field
for the string '2011 Scripting Games' it then sorts by the date InBox.
This sort is descending which puts the oldest messages at bottom of list.
The Select-Object cmdlet is then used to choose only the subject and ReceivedTime
properties and then only the last five messages are displayed. These last
five messages are the five oldest messages that meet the string.
.Notes
NAME: Get-OutlookInbox
AUTHOR: ed wilson, msft
LASTEDIT: 05/13/2011 08:36:42
KEYWORDS: Microsoft Outlook, Office
HSG: HSG-05-26-2011
.Link
Http://www.ScriptingGuys.com/blog
#Requires -Version 2.0
#>
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$folder.items |
Select-Object -Property Subject, ReceivedTime, Importance, SenderName
} #end function Get-OutlookInbox
you could try adding a param block to your script so you can run something like "Get-OutlookInbox -Mailbox ". It would look something like this:
param (
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=1)]
[string[]]$Mailbox=$env::USERNAME
)
So just to break it down a little, the actual name of the parameter is "Mailbox" and it is set to a default value that returns the current user running the script. You can override it by specifying one when you run the function. This will also accept a value from pipeline, meaning you can have an array of strings called "$aliases" and then run it like this: $aliases | Get-OutlookInbox
Is there a way to obtain list of all sent & received emails having suffix not equal to #gmail.com via PowerShell or VBScript and possibly store it in seperate text files.
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderSentMail)
$folder.items | Select-Object -Property Subject, SentOn, To
I was using the above PS to obtain sent mail, but not sure where to apply the condition.
Secondly, the subject is not appearing completely, it becomes ..... is there any way to obtain the full subject as well ?
I'm assuming you're running on a client computer not a server. If you've access to the exchange server there's powershell commandlets that are easy to use (New-MailboxExport).
$folder is a COM-Object
$folder.items property contains a collection of COM-Objects representing messages.
Since they're objects, you can use the object commands (Get-Help Object) to use their properties. You just need to dig a little more to apply your filter. Specifically one more level, to the properties of the items in $folder.items. Pipe $folder.items to Get-Member to get the full list of properties. $folder.items | gm. To, From, SentOn and Subject are all there.
$NonGmailMessages = $folder.items |
where-object { { $_.to -notcontains "gmail.com" } -and
{ $_.from -notcontains "gmail.com" } }
One way of handling collections like this is to do one massive filter like I just did. Or you can filter by stages.
$NonGmailMessages = $folder.items
$NonGmailMessages = $NonGmailMessages | where-object { { $_.to -notcontains "gmail.com" }
$NonGmailMessages = $NonGmailMessages | where-object { { $_.from -notcontains "gmail.com" }
Add further lines to further narrow your collection.
You can export this collection complete with all properties intact to a CSV:
$NonGmailMessages | Export-CSV -NoTypeInformation c:\temp\nonGmailMessages.csv
Or you can narrow the number of properties exported
$NonGmailMessages | Select-Object -Property From, To, SentOn, Subject | Export-CSV -NoTypeInformation c:\temp\nonGmailMessages.csv
-NoTypeInformation prevents the object type information from being listed at the start of the file. This will make it a more 'pure' CSV for use in PS or Excel or whatever. Plus the CSV IS a text based file format, as you wished.
I have an issue of certain administrators temporarily elevating users to Domain Admins for troubleshooting. Instead of removing said administrators from the picture, I have been asked to create a script to check the Domain Admins group nightly, and remove any users that do not belong there.
I need it to verify the Domain Admins group against a givin list in txt or csv. I could delete everone from the group nightly, then readd the desired users back, but this could create issues if someone is logging in or logging off when that happens.
Can anyone help with this? So far I have managed to export a list of users within the group by piping get-adgroup and export-csv. But I'm failing in my attempt to get the list of users and compare to an already existing list.
Here's a little script I just wrote for you. First you need to make a default text file with the default members. I named my file PreviousMembers.txt
Import-Module ActiveDirectory
Get-AdGroupMember "Domain Admins" | Select Name | Out-File C:\Scripts\PreviousMembers.txt
Now, save the following lines in ADGroupMembersDiff.ps1 and run it any time to get a current list of members. You'll need to make sure the files are in the same location that you specify below and the file names need to be the same, too.
Import-Module ActiveDirectory
$pattern = ".*"
Get-AdGroupMember "Administrators" | Select Name | Out-File C:\Scripts\CurrentMembers.txt
(Get-Content C:\Scripts\CurrentMembers.txt) | ? {$_.trim() -ne "Name" -and $_.trim() -ne "----" -and $_.trim() -ne "" } | Set-Content C:\Scripts\CurrentMembers.txt
(Get-Content C:\Scripts\PreviousMembers.txt) | ? {$_.trim() -ne "Name" -and $_.trim() -ne "----" -and $_.trim() -ne "" } | Set-Content C:\Scripts\PreviousMembers.txt
$comparedLines = Compare-Object (Get-Content C:\Scripts\PreviousMembers.txt) (Get-Content C:\Scripts\CurrentMembers.txt) -IncludeEqual | Sort-Object { $_.InputObject.ReadCount }
$lineNumber = 0
$comparedLines | foreach {
if($_.SideIndicator -eq "==" -or $_.SideIndicator -eq "=>")
{
$lineNumber = $_.InputObject.ReadCount
}
if($_.InputObject -match $pattern)
{
if($_.SideIndicator -eq "==")
{
$lineOperation = "No Change"
}
ElseIf($_.SideIndicator -eq "=>")
{
$lineOperation = "New User"
}
Elseif($_.SideIndicator -eq "<=")
{
$lineOperation = "Default User"
}
$HashChanges = #{
Line = $lineNumber
Operation = $lineOperation
Users = $_.InputObject
}
New-Object psobject -Property $HashChanges | select Users, Operation
}
}
Here's what the output will look like.
Users Operation
_____ ______
John Hancock New User
Thomas Edison Default User
George Washington New User
Thomas Jefferson No Change
Good Luck,
T|CK
MJOLINOR had the perfect suggestion. Here are some details about Restricted Groups...
By creating a Restricted Group GPO and linking it to the Domain Controllers OU, I have been able to add my desired group to the newly created GPO, tell that GPO which users belong in the desired group, and now, every 16 hours my desired group (Domain Admins) will be verified against the new GPO. So if users are members that are not part of the new GPO, they will be removed, and if users have been removed that are part of the new GPO, they will be added.
Thank you! And I hope this helps someone else.
I have only ever used Restricted groups to modify the memberships of local groups on client computers. I have never used it to modify the memberships of domain groups. In fact, Microsoft states that the intention of Restricted Group is not for Domain groups.
...Restricted Groups is a client configuration means and cannot be used with Domain Groups...