.bat file to restart explorer.exe sometimes fail

Page 4 of 6 FirstFirst ... 23456 LastLast

  1. Posts : 989
    Microsoft Windows 10 Home
       #31

    m18xr2 said:
    [/COLOR][/COLOR]
    im getting this in response from CMD. any idea what might be the cause?

    Attachment 303927
    My code is PowerShell, not cmd.exe. Open a PowerShell window.
      My Computer


  2. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #32

    KeithM said:
    My code is PowerShell, not cmd.exe. Open a PowerShell window.
    so run it with powershell instead of cmd? i'll try that thanks

    - - - Updated - - -

    KeithM said:
    My code is PowerShell, not cmd.exe. Open a PowerShell window.
    ran it in powershell, it restarted explorer.exe but did not reopen folders. i am not sure what I did wrong, pls help.
      My Computer


  3. Posts : 989
    Microsoft Windows 10 Home
       #33

    It can only reopen filesystem locations. Virtual folders such as This PC , Quick Access, and Libraries don't have a LocationURL value.

    Open some Explorer windows & check the ouput of this command:
    Code:
    $Shell.Windows() | select LocationName, LocationURL
      My Computer


  4. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #34

    KeithM said:
    It can only reopen filesystem locations. Virtual folders such as This PC , Quick Access, and Libraries don't have a LocationURL value.

    Open some Explorer windows & check the ouput of this command:
    Code:
    $Shell.Windows() | select LocationName, LocationURL
    yea this code shows the path of folder that i have opened, but it wasnt part of the original codes, where should this go?
      My Computer


  5. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #35

    KeithM said:
    It can only reopen filesystem locations. Virtual folders such as This PC , Quick Access, and Libraries don't have a LocationURL value.

    Open some Explorer windows & check the ouput of this command:
    Code:
    $Shell.Windows() | select LocationName, LocationURL
    Keith,

    I get the response

    Code:
    PS C:\> $Shell.Windows() | select LocationName, LocationURL
    You cannot call a method on a null-valued expression.
    At line:1 char:1
    + $Shell.Windows() | select LocationName, LocationURL
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    I take it you meant for it to be inserted within the original script?

    Denis
      My Computer


  6. Posts : 989
    Microsoft Windows 10 Home
       #36

    Try3 said:
    Keith,

    I get the response

    Code:
    PS C:\> $Shell.Windows() | select LocationName, LocationURL
    You cannot call a method on a null-valued expression.
    At line:1 char:1
    + $Shell.Windows() | select LocationName, LocationURL
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    I take it you meant for it to be inserted within the original script?

    Denis
    Denis,

    The initialization of $Shell was in my earlier reply:
    Code:
    $Shell =New-Object-ComObject shell.application
    This was follow-up/troubleshooting code.
      My Computer


  7. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #37

    Keith,

    Thanks.

    I've been playing around with the code and am a bit baffled by its behaviour. Sometimes it captures my open IE windows and sometimes it does not.

    I tested it by altering it a bit
    Code:
    $Shell = New-Object -ComObject shell.application
    $SavedPaths = ( $Shell.Windows() | select LocationName, LocationURL ) -ne ''
    #####Get-Process explorer | Stop-Process
    #####$SavedPaths | ForEach { explorer $_ }
    $SavedPaths | ForEach { echo $_ } | export-csv "D:\Desktop\ExplorerProcessList.csv" -append -NoTypeInformation
    pause
    so I could see what it was picking up instead of going through the whole procedure every time.

    The inclusion or omission of IE windows is not critical behaviour but the inconsistency is a puzzle [to me].

    Denis
      My Computer


  8. Posts : 989
    Microsoft Windows 10 Home
       #38

    Try3 said:
    Keith,

    Thanks.

    I've been playing around with the code and am a bit baffled by its behaviour. Sometimes it captures my open IE windows and sometimes it does not.

    I tested it by altering it a bit
    Code:
    $Shell = New-Object -ComObject shell.application
    $SavedPaths = ( $Shell.Windows() | select LocationName, LocationURL ) -ne ''
    #####Get-Process explorer | Stop-Process
    #####$SavedPaths | ForEach { explorer $_ }
    $SavedPaths | ForEach { echo $_ } | export-csv "D:\Desktop\ExplorerProcessList.csv" -append -NoTypeInformation
    pause
    so I could see what it was picking up instead of going through the whole procedure every time.

    The inclusion or omission of IE windows is not critical behaviour but the inconsistency is a puzzle [to me].

    Denis
    Not sure about the IE window behavior, but if I may....

    1. You can drop the -ne '' from:
      $SavedPaths = ( $Shell.Windows() | select LocationName, LocationURL ) -ne ''
      In the original code, it filtered out empty strings that were returned by virtual folders from the array of strings created by expanding LocationURL. You're now creating a custom object that will never match an empty string.
    2. $SavedPaths | ForEach { echo $_ } | export-csv "D:\Desktop\ExplorerProcessList.csv"...
      can be simplified to:
      $SavedPaths | export-csv "D:\Desktop\ExplorerProcessList.csv"
      My Computer


  9. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #39

    KeithM said:
    Not sure about the IE window behavior, but if I may....

    1. You can drop the -ne '' from:
      $SavedPaths = ( $Shell.Windows() | select LocationName, LocationURL ) -ne ''
      In the original code, it filtered out empty strings that were returned by virtual folders from the array of strings created by expanding LocationURL. You're now creating a custom object that will never match an empty string.
    2. $SavedPaths | ForEach { echo $_ } | export-csv "D:\Desktop\ExplorerProcessList.csv"...
      can be simplified to:
      $SavedPaths | export-csv "D:\Desktop\ExplorerProcessList.csv"
    ive tried a few times and unlike try3 i have had no success. I opened up several folders (not My Computer type, but c drive, system 32, d drive etc) and they all show and list under

    Code:
     $Shell.Windows()|selectLocationName,LocationURL 


    but with original code they just dont get restored upon process restart. what could be the cause?

    $Shell = New-Object -ComObject shell.application
    $SavedPaths = ( $Shell.Windows() | select -expand LocationURL )
    Get-Process explorer | Stop-Process
    $SavedPaths | ForEach { explorer $_ }
      My Computer


  10. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #40

    This is of no help to you at all but I've compared what you are using with what I'm using and they are exactly the same.

    RestartExplorerWithFolders.ps1
    Code:
    $Shell = New-Object -ComObject shell.application
    $SavedPaths = ( $Shell.Windows() | select -expand LocationURL )
    Get-Process explorer | Stop-Process
    $SavedPaths | ForEach { explorer $_ }

    I have also run them by pasting those commands into a PowerShell window - as shown, there are no error responses.
    Code:
    PS C:\Users\Denis> $Shell = New-Object -ComObject shell.application
    PS C:\Users\Denis> $SavedPaths = ( $Shell.Windows() | select -expand LocationURL )
    PS C:\Users\Denis> Get-Process explorer | Stop-Process
    PS C:\Users\Denis> $SavedPaths | ForEach { explorer $_ }
    PS C:\Users\Denis>

    As you noted above, it works for me [both as a ps1 file & as a set of commands in the PS window]. My FE folders get reopened afterwards

    Denis
      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 02:44.
Find Us




Windows 10 Forums