converting a vbs restriction in outlook to powershell

Alfred Mühlleitner 0 Reputation points
2023-10-14T17:37:15.0566667+00:00

How can I convert this part of a VB-Script to a working powershell command?

Set objOutlook = CreateObject("Outlook.application")
            Set objNameSpace = objOutlook.GetNameSpace("MAPI")
            Set objFolder = objNameSpace.GetDefaultFolder(olFolderCalender)
			
			dtTwoWeeks = DateAdd("d", +14, date)
            dtNextDay  = DateAdd("d", +1, date)
			strRestriction = "[Start] >= '" & dtNextDay & "' AND [Start] <= '" & dtTwoWeeks & "'" & "AND [IsRecurring] = False"

            Set MyItems = objFolder.Items.Restrict(strRestriction)	
			MyItems.Sort("[Start]")

            For Each CurrentAppointment in MyItems 
...
Outlook
Outlook
A family of Microsoft email and calendar products.
3,079 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,598 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,132 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 41,006 Reputation points
    2023-10-14T17:56:52.0666667+00:00

    How can I convert this part of a VB-Script to a working

    VBA and PowerShell are completely different, there is nothing to "convert", you have to rewrite to code from skretch.

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. KOZ6.0 4,895 Reputation points
    2023-10-15T03:59:35.49+00:00

    I asked ChatGPT.I do not guarantee results.

    # Create Outlook application object
    $objOutlook = New-Object -ComObject Outlook.Application
    $objNamespace = $objOutlook.GetNameSpace("MAPI")
    $objFolder = $objNamespace.GetDefaultFolder(2) # Numeric value for olFolderCalendar is 2
    
    # Get date for two weeks later
    $dtTwoWeeks = (Get-Date).AddDays(14).ToString("yyyy-MM-dd")
    # Get date for the next day
    $dtNextDay = (Get-Date).AddDays(1).ToString("yyyy-MM-dd")
    # Set the search criteria
    $strRestriction = "[Start] >= '$dtNextDay' AND [Start] <= '$dtTwoWeeks' AND [IsRecurring] = False"
    
    # Get search results
    $MyItems = $objFolder.Items.Restrict($strRestriction)
    # Sort by start date
    $MyItems.Sort("[Start]")
    
    # Process the search results
    foreach ($CurrentAppointment in $MyItems) {
        # Add your code here using $CurrentAppointment for processing
    }
    
    0 comments No comments