Advertisement
wetyukmnbxc

Untitled

Mar 21st, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Write a powershell script to read the email subject and email content stored in a outlook pst file.
  2.  
  3. As an AI language model, I don't have access to your local system resources. However, I can provide you with a sample PowerShell script that you can use as a reference to read the email subject and content stored in an Outlook PST file.
  4.  
  5. Here is the sample PowerShell script:
  6.  
  7. ```
  8. # Load the Outlook COM object
  9. Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
  10.  
  11. # Specify the path of the PST file
  12. $pstFilePath = "C:\Users\username\Documents\Outlook Files\archive.pst"
  13.  
  14. # Create a new Outlook application object
  15. $outlook = New-Object -ComObject Outlook.Application
  16.  
  17. # Open the PST file
  18. $pst = $outlook.Session.AddStore($pstFilePath)
  19.  
  20. # Get all the folders in the PST file
  21. $folders = $pst.GetRootFolder().Folders
  22.  
  23. # Loop through each folder and get all the emails
  24. foreach ($folder in $folders) {
  25.    $emails = $folder.Items | Where-Object { $_.Class -eq 43 }
  26.  
  27.    # Loop through each email and display its subject and content
  28.    foreach ($email in $emails) {
  29.        Write-Host "Subject: $($email.Subject)"
  30.        Write-Host "Content: $($email.Body)"
  31.        Write-Host ""
  32.    }
  33. }
  34.  
  35. # Close the PST file and release resources
  36. $pst.Store.Close()
  37. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($pst) | Out-Null
  38. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
  39. ```
  40.  
  41. In this script, we first load the Outlook COM object using `Add-Type`. We then specify the path of the PST file using `$pstFilePath`.
  42.  
  43. Next, we create a new Outlook application object using `New-Object -ComObject Outlook.Application`. We then open the PST file using `$outlook.Session.AddStore($pstFilePath)`.
  44.  
  45. We get all the folders in the PST file using `$pst.GetRootFolder().Folders` and loop through each folder to get all the emails. We filter out only email items using `Where-Object { $_.Class -eq 43 }`.
  46.  
  47. Finally, we loop through each email item and display its subject and content using `Write-Host`.
  48.  
  49. After we are done reading from the PST file, we close it using `$pst.Store.Close()` and release all resources using `[System.Runtime.Interopservices.Marshal]::ReleaseComObject($pst)` and `[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook)`.
  50.  
  51. Note that this script requires Microsoft Office installed on your system with Outlook configured to use a profile that has access to your PST files.
  52.  
  53. Daily quota: 90/100
  54. ENG | ES | عربي | 中文 | فارسی
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement