How to use PowerShell to switch default app (from pre-determined list)

Page 2 of 2 FirstFirst 12

  1. Posts : 61
    Thread Starter

    Thanks for free and useful information. For a novice end user, I need confirmation Last Access Time is appropriate because Recent Files of File Explorer does not show date information of following:
    1. accessed
    2. acquired
    3. created
    4. last saved
    5. received
    How to use PowerShell to switch default app (from pre-determined list)-powershell-script-get-most-recent-exported-pdf.gif
      My Computer

  2. Posts : 840
    Windows 7

    #2, 4 and 5 are optional tags that applications can add to file properties.

    I doubt those tags are needed to solve this problem. When a major application like Adobe Acrobat creates a new file, the Recent Items folder is usually updated. But you can look for other answers if my script doesn't make you satisfied.
      My Computer

  3. Posts : 61
    Thread Starter

    Word cannot open PDF Exception setting "Open": Cannot convert

    Thanks, your update works to get the most Recent PDF but I need further help because Word cannot open this file (line 82)
    How to use PowerShell to switch default app (from pre-determined list)-powershell-script-fail-open-pdf.gif
    1. Open the Recent Folder, and look for the last opened *.pdf filename.
    2. Set DisableConvertPDFWarning reg value = 1, if needed.
    3. Run Word using the normal window size, and open the PDF file.
    4. Wait until conversion (file open) is done, then resize Word to full screen and set to foreground.
    How to enable execution of PowerShell scripts?
    set-executionpolicy remotesigned -Scope CurrentUser
    Add-Type @"
        using System;
        using System.Runtime.InteropServices;
        public class WinAp {
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
    $objShell = New-Object -ComObject WScript.Shell
    2023-06-08 now uses the target's file Last Access time, instead of checking the *.lnk file's time
    $Last_PDF = (Get-ChildItem $env:APPDATA\Microsoft\Windows\Recent -Filter *.lnk |`
        ForEach-Object {
            $Target = $objShell.CreateShortcut($_.FullName).TargetPath
            if ($Target -match ".pdf$") {
                    Filename = $Target
                    LastAccess = (Get-ChildItem $Target).LastAccessTime
        } | Sort-Object LastAccess -Descending | select Filename -First 1)
    if ($Last_PDF -eq $null) {
    2023-06-06 $Last_PDF returns file name that is not shown in File Explorer Recent;
    Word reports no such file.
    $Last_PDF = (Get-ChildItem $env:APPDATA\Microsoft\Windows\Recent -Filter *.lnk | Sort-Object LastAccessTime -Descending |`
        ForEach-Object {
            $Target = $objShell.CreateShortcut($_.FullName).TargetPath
            if ($Target -match ".pdf$") { $Target }
        } | select -First 1)
    if ($Last_PDF -eq $null) {
    $objWord = New-Object -ComObject Word.Application
    $objWord.Visible = $true
    $objWord.WindowState = "wdWindowStateNormal"
    $Version = $objWord.Version
    $RegPath = "HKCU:\SOFTWARE\Microsoft\Office\$Version\Word\Options"
    if ((Get-ItemPropertyValue -Path $RegPath -Name DisableConvertPDFWarning -ErrorAction Ignore) -eq 0) {
        $null = New-ItemProperty -Path $RegPath -Name DisableConvertPDFWarning -Value 1 -PropertyType DWORD -Force
    $def = [Type]::Missing
    $Doc = $objWord.Documents.Open($Last_PDF, $false, $def, $false)
    $objWord.WindowState = "wdWindowStateMaximize"
    $Proc = Get-Process | where { $_.ProcessName -match "WINWORD" }
    $hwnd = $Proc.MainWindowHandle
      My Computer

  4. Posts : 840
    Windows 7

    My mistake, I forgot to add the bold part.
    } | Sort-Object LastAccess -Descending | select Filename -First 1).Filename
      My Computer


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 21:45.
Find Us

Windows 10 Forums