Restart explorer.exe Process in Windows 10  

Page 5 of 7 FirstFirst ... 34567 LastLast

  1. Posts : 519
    Win7 Pro X64, Win10 Pro x640
       #40

    Matthew Wai said:
    In KeithM's script, replace the first line with the following line:

    $Shell = New-Object -ComObject shell.application

    Note that a dollar sign has been added at the beginning. That will also solve the problem mentioned in your question 3.

    @KeithM's script does not work as intended. Something is possibly missing.
    The PowerShell error message is gone but the PowerShell window is still open after the command is executed.
    @KeithM can you help with your restart explorer script please? It doesn't reopen the last used window, just defaults to ThisPC or Documents.

    Thanks!
      My Computer


  2. Posts : 7,606
    Windows 10 Home 20H2
       #41

    bamajon1974 said:
    The PowerShell error message is gone but the PowerShell window is still open after the command is executed.
    Using VBScript to run PowerShell will stop the window appearing.

    Code:
    F="D:\KeithM.ps1":Set X=CreateObject("Wscript.shell")
    X.run("PowerShell -executionpolicy bypass -file "&""""&F&""""),0
      My Computer


  3. Posts : 989
    Microsoft Windows 10 Home
       #42

    bamajon1974 said:
    @KeithM can you help with your restart explorer script please? It doesn't reopen the last used window, just defaults to ThisPC or Documents.

    Thanks!
    Matthew Wai said:
    @KeithM's script does not work as intended. Something is possibly missing.
    Boy! Lie down for a nap, wake up a few weeks later, and I find my name being taken in vain!!!

    In my defense, aside from the $Shell transcription error:
    • The code snippet that came from another post was offered there ass a quick example with the words "...something like...". It wasn't intended for distribution.
    • My familaariy with Shell.Application com objects has been mainly with the Folder & FolderItem objects, but I had only used the Windows() collection for closing Explorer windows, and LocationURL was the only apparent information avaiaable on a cursory examination of the InternetExplorer objects that comprise the collection.
    • The code did work within the severe limitations noted: it only worked for folders that displayed file system locations and namespace paths were lost ( This PC\Documemts reopens to C:\Users\<UserName>\Documents )


    But with a fresh look, more research, and the all-important playtime in the PowerShell console, I have two improved options to offer. The first is just a few lines, very similar to the code that was posted earlier in this thread, but it handles all the permanent virtual folders, Quick Access, This PC, Network, Control Paneel, etc. as well as file sytem locations. But again, the Namespace paths are lost. For some users, that may not matter. The second option conquors the Namespace path issue. Both still have limitations regarding Libraries and neither can restore Search Results... folders.

    The critical info I was unaware of earlier was how to obtain the Folder/FolderItem obects associated with the folder from the InternetExplorer objects returned in the ShellWindows collection. It turns out the associated Folder object is a property of the window's Document property.



    Option 1 - Short & Sweet:

    Code:
    $Shell      = New-Object -Com shell.Application
    $SavedPaths = ( $Shell.Windows() | ? Name -eq 'File Explorer' ).Document.Folder.Self.Path
    Get-Process explorer | Stop-Process
    $SavedPaths | %{ $shell.Open(( $_ -replace ('^::','Shell:::'))) }



    Option 2 - NameSpace Matters:

    So while the Namespace path is not directly available from any of the associated objects, every Folder object has a ParentFolder property which itself is a Folder object. So we can construct the Namespace path for a folder by working our way back from the folder to the root of the Shell namespace, the virtual Desktop. When it's time to restore the folder, we start from the Desktop and use the Namespace path elements to obtain the FolderItem associated with the saved fodler. So this version defines two recursvie functdions to accomplish those tasks.

    A FolderItem is roughly eqivalent in functionaality to a folder that appears as asubfolder in the Folder Pane of Explorer, you can invoke context menu commands like Open, Copy, etc. So we can "doulbe-click" the FolderItem ( which is internally Namespace-aware) to open Explorer via the same path the original folder used.

    Code:
    Function Get-NSPath ( $oFolder ) {
        If ( $oParentFolder = $oFolder.ParentFolder ) {
            (( Get-NSPath $oParentFolder ) , $oFolder.Title -join '\' ).TrimStart('\')
        }
    }
    
    Function Open-NSFolder ( $ParentFolder , $ChildPath ) {
        If ( $Null -eq $ChildPath ) {                            # Target is rooted Desktop
            $ParentFolder.Self.InvokeVerb()                      # Open target folder
        } Else {
            $ChildItem = $ParentFolder.Items() |
              ? IsFolder -eq $True |
                ? Name -eq $ChildPath.Split('\')[0]
            If ( $ChildPath -notMatch '\\' ) {                   # Path leaf found
                $ChildItem.InvokeVerb()                          # Open target folder
            } Else {                                               ### ELSE Recurese ###
                Open-NSFolder $ChildItem.GetFolder ( $ChildPath -replace '^.+?\\' , '' )
            }
        }
    }
    
    ###### Execution begins here ########
    
    $Shell           = New-Object -Com shell.Application
    $SavedFolderInfo = ( $Shell.Windows() |
      ? Name -eq 'File Explorer').Document.Folder |
         ForEach{ [PSCustomObject]@{
            'IsFileSystem'   = $_.Self.IsFileSystem
            'FolderItemPath' = $_.Self.Path
            'NameSpacePath'  = Get-NSPath $_
    }}
    
    Get-Process explorer | Stop-Process
    
    $oDesktop = $Shell.NameSpace("shell:Desktop")
    $SavedFolderInfo | %{
        If ( $_.IsFileSystem ) {
            Open-NSFolder $oDesktop $_.NameSpacePath
        } Else {
            $Shell.Open( ($_.FolderItemPath -replace ('^::','Shell:::')) )
        }    
    }
    Will add additional notes later...
      My Computer


  4. Posts : 519
    Win7 Pro X64, Win10 Pro x640
       #43

    KeithM said:
    Boy! Lie down for a nap, wake up a few weeks later, and I find my name being taken in vain!!!

    In my defense, aside from the $Shell transcription error:
    • The code snippet that came from another post was offered there ass a quick example with the words "...something like...". It wasn't intended for distribution.
    • My familaariy with Shell.Application com objects has been mainly with the Folder & FolderItem objects, but I had only used the Windows() collection for closing Explorer windows, and LocationURL was the only apparent information avaiaable on a cursory examination of the InternetExplorer objects that comprise the collection.
    • The code did work within the severe limitations noted: it only worked for folders that displayed file system locations and namespace paths were lost ( This PC\Documemts reopens to C:\Users\<UserName>\Documents )


    But with a fresh look, more research, and the all-important playtime in the PowerShell console, I have two improved options to offer. The first is just a few lines, very similar to the code that was posted earlier in this thread, but it handles all the permanent virtual folders, Quick Access, This PC, Network, Control Paneel, etc. as well as file sytem locations. But again, the Namespace paths are lost. For some users, that may not matter. The second option conquors the Namespace path issue. Both still have limitations regarding Libraries and neither can restore Search Results... folders.

    The critical info I was unaware of earlier was how to obtain the Folder/FolderItem obects associated with the folder from the InternetExplorer objects returned in the ShellWindows collection. It turns out the associated Folder object is a property of the window's Document property.



    Option 1 - Short & Sweet:

    Code:
    $Shell      = New-Object -Com shell.Application
    $SavedPaths = ( $Shell.Windows() | ? Name -eq 'File Explorer' ).Document.Folder.Self.Path
    Get-Process explorer | Stop-Process
    $SavedPaths | %{ $shell.Open(( $_ -replace ('^::','Shell:::'))) }



    Option 2 - NameSpace Matters:

    So while the Namespace path is not directly available from any of the associated objects, every Folder object has a ParentFolder property which itself is a Folder object. So we can construct the Namespace path for a folder by working our way back from the folder to the root of the Shell namespace, the virtual Desktop. When it's time to restore the folder, we start from the Desktop and use the Namespace path elements to obtain the FolderItem associated with the saved fodler. So this version defines two recursvie functdions to accomplish those tasks.

    A FolderItem is roughly eqivalent in functionaality to a folder that appears as asubfolder in the Folder Pane of Explorer, you can invoke context menu commands like Open, Copy, etc. So we can "doulbe-click" the FolderItem ( which is internally Namespace-aware) to open Explorer via the same path the original folder used.

    Code:
    Function Get-NSPath ( $oFolder ) {
        If ( $oParentFolder = $oFolder.ParentFolder ) {
            (( Get-NSPath $oParentFolder ) , $oFolder.Title -join '\' ).TrimStart('\')
        }
    }
    
    Function Open-NSFolder ( $ParentFolder , $ChildPath ) {
        If ( $Null -eq $ChildPath ) {                            # Target is rooted Desktop
            $ParentFolder.Self.InvokeVerb()                      # Open target folder
        } Else {
            $ChildItem = $ParentFolder.Items() |
              ? IsFolder -eq $True |
                ? Name -eq $ChildPath.Split('\')[0]
            If ( $ChildPath -notMatch '\\' ) {                   # Path leaf found
                $ChildItem.InvokeVerb()                          # Open target folder
            } Else {                                               ### ELSE Recurese ###
                Open-NSFolder $ChildItem.GetFolder ( $ChildPath -replace '^.+?\\' , '' )
            }
        }
    }
    
    ###### Execution begins here ########
    
    $Shell           = New-Object -Com shell.Application
    $SavedFolderInfo = ( $Shell.Windows() |
      ? Name -eq 'File Explorer').Document.Folder |
         ForEach{ [PSCustomObject]@{
            'IsFileSystem'   = $_.Self.IsFileSystem
            'FolderItemPath' = $_.Self.Path
            'NameSpacePath'  = Get-NSPath $_
    }}
    
    Get-Process explorer | Stop-Process
    
    $oDesktop = $Shell.NameSpace("shell:Desktop")
    $SavedFolderInfo | %{
        If ( $_.IsFileSystem ) {
            Open-NSFolder $oDesktop $_.NameSpacePath
        } Else {
            $Shell.Open( ($_.FolderItemPath -replace ('^::','Shell:::')) )
        }    
    }
    Will add additional notes later...
    Ok. On vacation now. Will examine more closely afterward. Thank you for the reply!
      My Computer


  5. Posts : 989
    Microsoft Windows 10 Home
       #44

    bamajon1974 said:
    Ok. On vacation now. Will examine more closely afterward. Thank you for the reply!
    Cool. I'll be posting updated code soon. I've figured out how to re-open Searaces. Wondering if I should try a version that saves/restores the size & position of each window.

    Bear in mind this is a work in progress -- no guarantees, but not risky -- no data dletion or modification. Welcome all feedback from those running the code.
      My Computer


  6. Posts : 519
    Win7 Pro X64, Win10 Pro x640
       #45

    Absolutely! If you don't mind please!
      My Computer


  7. Posts : 7,606
    Windows 10 Home 20H2
       #46

    KeithM said:
    Option 2 - NameSpace Matters:
    I just ran the above via the following VBScript, and it worked as intended without a window appearing.

    Code:
    F="D:\KeithM.ps1":Set X=CreateObject("Wscript.shell")
    X.run("PowerShell -executionpolicy bypass -file "&""""&F&""""),0
      My Computer


  8. Posts : 989
    Microsoft Windows 10 Home
       #47

    Matthew Wai said:
    I just ran the above via the following VBScript, and it worked as intended without a window appearing.

    Code:
    F="D:\KeithM.ps1":Set X=CreateObject("Wscript.shell")
    X.run("PowerShell -executionpolicy bypass -file "&""""&F&""""),0
    Congratulations! You're my first beta tester!

    I only have my laptop for testing, so I'm glad for feedback from other users.

    I'm pretty confident in the individual functions at this point, but timing the code to not "get ahead of itself" can be tricky.

    It's a good idea to test code interactively at the console (not the ISE) before trying to wrap it in anything else.

    Does it wrork when you open a PS console window, paste the code & run???

    If not, return to the open console window and see if the saved info is correct:
    Code:
    $savedFolderInfo | fl


    If it works pasted into the console, then save as a .ps1 file and test by invoking with dot-sourcing:
    Code:
    . <‹Path to .ps1 file>
    Will await your reply...
      My Computer


  9. Posts : 7,606
    Windows 10 Home 20H2
       #48

    KeithM said:
    Does it wrork when you open a PS console window, paste the code & run???
    No folders were opened after I ran the code from a window. However, after I clicked on the window and just pressed "Enter" without typing anything, the folders were opened.

    KeithM said:
    If it works pasted into the console, then save as a .ps1 file and test by invoking with dot-sourcing:

    Code:
    . <‹Path to .ps1 file>
    The path is "D:\KeithM.ps1". What command should be run so as to test it by invoking with dot-sourcing?
      My Computer


  10. Posts : 989
    Microsoft Windows 10 Home
       #49

    Matthew Wai said:
    No folders were opened after I ran the code from a window. However, after I clicked on the window and just pressed "Enter" without typing anything, the folders were opened.
    That's normal. No command(s) typed (or pasted) into the console execute until you press Enter

    The path is "D:\KeithM.ps1". What command should be run so as to test it by invoking with dot-sourcing?
    The dot (.) followed by a sp ace and then the path is the command -- that's why it's called dot-sourcing.
    Code:
    . 'D:\KeithM.ps1'
      My Computer


 

Tutorial Categories

Restart explorer.exe Process in Windows 10 Tutorial Index Network & Sharing Instalation and Upgrade Browsers and Email General Tips Gaming Customization Apps and Features Virtualization BSOD System Security User Accounts Hardware and Drivers Updates and Activation Backup and Restore Performance and Maintenance Mixed Reality Phone


  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 23:36.
Find Us




Windows 10 Forums