Powershell Set Unread flag for Outlook Desktop emails - powershell

I am trying to read emails from a folder in Outlook Desktop using powershell, and I can do that with the code below.
But is there any way by which I could also set the Unread property to $False for those emails
directly in outlook ? ( not in the objects stored in variables )
Browsing the website I found this: https://stackoverflow.com/a/34161683/13568461 which says to use MailItem.Save but I cant figure it out how to update my code to make use of that.
Much appreciated if anyone can help with that piece of code. Thank you in advance
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$Folder_AI = $namespace.Folders.Item('user#domain').Folders.Item('AI-ServiceDesk-Servers')
$Count_ItemsUnread = $Folder_AI.UnReadItemCount
$All_Folder_Items = $Folder_AI.Items | Select-Object -Property UnRead, Body
$All_Folder_Items | ForEach-Object {
$Email_Status = $PSItem.Unread
$Email_Body = $PSItem.Body
$Email_Status
}

But is there any way by which I could also set the Unread property to $False for those emails directly in outlook ? ( not in the objects stored in variables )
No, you need to get an item to set up a property. The Save method is required to make changes permanent and avoid any dialogs for saving changes made programmatically in Outlook.
When dealing with Exchange accounts in Outlook you may reach the limit of concurrently opened items if you iterate over all items in the folder/store. That is why Find/FindNext or Restrict methods are recommended. They allow getting items that correspond to your search criteria and iterate over them only. Read more about these methods in the article that I wrote for the technical blog:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder

Related

How to Delete Outlook Item in Archive.pst

I'm stucked, i'm able to connect a pst file in Outlook, I would like to loop through all folders / search a certain email with a certain subject and then, delete it. I've searched through the web but don't know how to start, can someone explain me plz ? I'm lost with $namespace.folders.item.
Clear-host
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$folders = "C:\Tools\Archive.pst"
$namespace.AddStore($folder)
I'm thinkin' about this but I know i'm wrong :
$email = $folders.Folders.items | Where-Object {$_.Subject -like "*alert*"} OR
Foreach ($Folder in $SubFolder.Folders)
{
$ItemsToDelete = $Folder.Items | Where-Object -Property subject -like "*alert*"
foreach ($item in $ItemsToDelete)
{
$item.Delete()
}
}
(all articles at Microsoft's are related to VB which I don't know and i'm starting Powershell : https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.addstore)
Thx for your advices.
After adding a new store in Outlook you can find it in the Stores collection which you may get by using the Stores property of the Namespace class. Use the Stores and Store objects to enumerate all folders and search folders on all stores in the current session. The following VBA sample code shows how you can use these objects to iterate over all stores and folders in Outlook:
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
End Sub
Note, the Outlook object model is common for all programming languages, so you may find the sequence of property and methods calls in the sample shown above.
To find items that correspond to your conditions you need to use the Find/FindNext or Restrict methods of the Items class. In that case you will get only items that correspond to the search criteria and then you can iterate over them only. Read more about these methods in the articles I wrote for the technical blog:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
If you need to find items in multiple folders I'd recommend choosing the AdvancedSearch method of the Application class instead. The key benefits of using the AdvancedSearch method in Outlook are:
The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
You can stop the search process at any moment using the Stop method of the Search class.
See Advanced search in Outlook programmatically: C#, VB.NET for more information.

How do I get to a specific table in an Access Database using PowerShell

Any help would be greatly appreciated. I am trying to open an Access Database and then open a specific table named "UserInfo" using PowerShell. At that point, I would modify the records within that table. As for now, I simply want to get to the table. I have this very basic code I saw somewhere else and modified a tad. I have looked everywhere and can't find a solution to getting to this table. Ultimately, I think that I am looking for a method to use in conjunction with DoCmd?
$filename = "Z:\Database2.accdb"
$accdb = New-Object -ComObject access.application
$accdb.Visible = $true
$accdb.OpenCurrentDatabase($filename)
$accdb.DoCmd

How to Use PowerShell Create a new Distribution List( Contact Group) on Outlook 2010 Contacts

I want to use PowerShell script to create a new outlook Contact group in contacts.
So far, I have something like this. What do I do next?
$outlook = new-object -com Outlook.Application
$contacts = $outlook.Session.GetDefaultFolder(10)
$newcontact = $contacts.Items.Add()
You need to specify that you want the DL list:
$dl= $contacts.Items.Add("IPM.DistList")
I found some help here; Add an email to outlook distribution list using powershell
If you scroll through that there are two chunks. One if the list member is already an outlook contact and another if they are not.

Powershell automation, email MS-Access Query

At an impasse, (a complete loss as to where to go next)
I am building a powershell script to automate a number of manual tasks one of which is to run a query within access and then email that object to a number of recipients.
The closest I have got to was running
$Acc = New-Object -com Access.Application
$Acc.OpenCurrentDataBase("L:\Operations Database\OPS\OPS-V2.0.accdb")
##Test email object
$Acc.DoCmd.RunMacro("EmailLiveFNZ")
This ran the macro that I had produced in Access but I was then required to manually click on an Outlook warning box so it has not resolved the issues.
I have then started playing around with various versions of the following script but can't get it to work.
$Acc = New-Object -com Access.Application
$Acc.OpenCurrentDataBase("L:\Operations Database\OPS\OPS-V2.0.accdb")
##Test email object
$Acc.DoCmd.SendObject acSendQuery, "LiveFNZFundXfer", acFormatXLS, _
"email#address.com", , , _
"Live Transfers", , False
I am assuming that I can send the query out using powershell automatically I just can't find the assistance I need online...
Alternatively if anyone knows how to save an access object to a specific folder location on a PC I could have the script email the file from there, I can't work it out...
Thanks for your help!
Regards
R
I also tried;
$Acc = New-Object -com Access.Application
$Acc.OpenCurrentDataBase("L:\Operations Database\OPS\OPS-V2.0.accdb")
##Test export object
$Acc.DoCmd.OpenQuery("LiveFNZFundXfer") | Export-CSV "C:\LiveFNZFundXfer.csv"
This compiles without errors but does not export anything

Invoke verb on multiple files via Shell.Application

I'm using "Shell.Application" COM object for some automation via PowerShell and I'm stuck on situation where I need to invoke verb on multiple items in folder. Basically I need to simulate situation where user selects multiple files/folders and calls verb from shell menu.
I can do it without any problem on single item, but I haven't found method how to call it on multiple items. Any idea how to do it?
Edit:
This is how I call it on single item:
$shell = New-Object -Com "Shell.Application"
$shell.Namespace($Path).Self.InvokeVerb("some action")
You can use InvokeVerbEx to execute on all files in a folder:
$shell.Namespace("C:\test").Items().InvokeVerbEx()
For particular files in a folder, I would think the best way is to put the single item code that you have in a loop:
$path1, $path2 | %{ $shell.Namespace($_).Self.InvokeVerb() }
The Filter method may be of use too