1

I'd like to temporarily map a drive with powershell (I found this post which gave me the commands I need) and then open a windows explorer window displaying that drive. I think I can do this using ii MyDrive:\MyPath\.

I would like, when my windows explorer window is closed by the user, to dismap the network drive previously mapped, and then stop the powershell script.

Question : Is there any way I can check the state of windows explorer window ?


I should mention that I never worked with powershell before, so I don't have much knowledge about it.

Community
  • 1
  • 1
  • 2
    This isn't easy. Even when starting a new instance, it will only be around shortly, telling another process to open that window. So you can't easily see whether the one you started is still around. This depends on the setting "Launch folder windows in a separate process" which is off by default. Another option is to look for a window with the proper title, but that's a brittle approach, may break in the future, and if they open a different folder in that window, you'd wrongly conclude it's gone. TL;DR: PowerShell or not, It's not trivial to get right and is more likely to go wrong than right. – Joey Oct 06 '16 at 10:02

2 Answers2

3

Hey I didn't get the solution, but maybe my approach is the way to your solution.

I played a bit with the .net functions and get the process. The process is directly gone, after calling the explorer...

So the base-function is

[System.Diagnostics.Process]::Start("explorer.exe", "C:\")

what returns the process:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                                                                                                                                                      
-------  ------    -----      ----- -----   ------     --  -- -----------                                                                                                                                                      
      4       2      392       1140    10     0,00   6328   1 explorer  

There a some more functions in it like:

[System.Diagnostics.Process]::Start("explorer.exe", "C:\").WaitForExit()

But nothing happens :(

If you put the result into a variable, you can discover the functions and attributes:

$tmp = [System.Diagnostics.Process]::Start("explorer.exe", "C:\")

Type $tmp. and hit Strg + Space in ISE

Then I took the process via get-process cmd

$process = get-process -Id ([System.Diagnostics.Process]::Start("explorer.exe", "C:\")).Id

This results me a process like:

> $process

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                                                                                                                                                      
-------  ------    -----      ----- -----   ------     --  -- -----------                                                                                                                                                      
      0       1      384        120     6            8396   1 explorer                                                                                                                                                         

But the same: this process is gone immidiatly :(

I played around and got this resolution:

$path = "C:\temp\test"
$split = $path.split("\\")
$mainWindowTitle = $split[$split.Length - 1]
$tmp = [System.Diagnostics.Process]::Start("explorer.exe", $path)
sleep -Seconds 2
Get-Process | ? ProcessName -eq explorer | select * | ? MainWindowTitle -eq $mainWindowTitle | select Id

With this code, I get the Process you're looking for. Let me explain that to you.

First I need the path, and the last subdirectory in it. Every explorer Process has an mainWindowTitle and its value is the name of the opened directory.

So I started the explorer woth the specific path, and splited out the last subdirectory. You need to sleep for 1 or 2 seconds, because the Process take a while, before getting into the process list. Then I searched for every explorer with the windowMainTitle equals the last subdirectory.

So now you got the processId, with allows you to create a while loop, with a sleep in it, where you check if the process still exists.

The only problem is, that the window can't be opened twice or a directory with the same name.

But maybe you can use this code. Just play around and see what you can figure out.

Edit:

This code works for me :)

$path = "C:\temp\test"
$split = $path.split("\\")
$mainWindowTitle = $split[$split.Length - 1]
$tmp = [System.Diagnostics.Process]::Start("explorer.exe", $path)
sleep -Seconds 2
$processId = (Get-Process | ? ProcessName -eq explorer | select * | ? MainWindowTitle -eq $mainWindowTitle | select Id).Id
while((get-process -Id $processId ).Length -gt 0){
  sleep -Seconds 1
  $processId = (Get-Process | ? ProcessName -eq explorer | select * | ? MainWindowTitle -eq $mainWindowTitle | select Id).Id
}
# All code after will be executed after window was closed
write-host "Window Closed"

Greetz Eldo.Ob

Eldo.Ob
  • 774
  • 4
  • 16
  • Question : I tried every shortcut usually used in other IDE such as Ctrl+Space, but it doesn't work on my powershell ISE, and I don't understand what you mean by "Strg". Could you develop what you mean ? – Valentin O. Oct 07 '16 at 08:50
  • oh sorry strg is the germen one xD I ment ctrl. It's a kind of autocomplete. If you type get- and then press ctrl + space it should give you a list of availible commands. But it only works if no script is running. – Eldo.Ob Oct 07 '16 at 08:59
  • x) It troubles me then, because Ctrl+Space just put a space in my script... I have no script running. I know this shortcut should work because I used it before on a virtual machine running windows server 2008 R2... EDIT: Ctrl+Space isn't listed in the shortcuts list of windows powershell help (displayed with F1) – Valentin O. Oct 07 '16 at 09:04
  • hmm intellisense was the word I'm looking for: [Intellisense](https://blogs.msdn.microsoft.com/powershell/2012/06/12/intellisense-in-windows-powershell-ise-3-0/) – Eldo.Ob Oct 07 '16 at 09:10
  • Well I'm woking on win7 sp1, your article concerns OSs since win8.. x) – Valentin O. Oct 07 '16 at 09:46
  • I'm working on win7 too, and it works - but try this one: [Windows Management Framework 5.0](https://www.microsoft.com/en-us/download/details.aspx?id=50395) install this one for Powershell 5.0. – Eldo.Ob Oct 07 '16 at 09:50
-1

You can use net use. like this:

net use K: \\servername\sharename /persistent:no

After a logoff the drive is not mapped anymore

guiwhatsthat
  • 2,349
  • 1
  • 12
  • 24
  • They want to remove the mapping after the explorer window is closed, not on logoff. – Joey Oct 06 '16 at 09:52
  • Do you start the explorer window with powershell? – guiwhatsthat Oct 06 '16 at 09:57
  • Basically I want to connect a user to a permission-protected drive, but I don't want to give him the permission. Therefore I need to connect him with password, but I don't want him to know the password. I thought using powershell would allow me to do this. – Valentin O. Oct 06 '16 at 10:00
  • Is it nessecary that the drive is disconnected after closing the window? – guiwhatsthat Oct 06 '16 at 10:01
  • @guiwhatsthat Yes I start the explorer window with powershell (either `explorer` or `ii c:\folder\` satisfies me) – Valentin O. Oct 06 '16 at 10:02
  • @guiwhatsthat no necessarily, I can tell people to disconnect themselves after their manipulations, so the drive won't be mapped anymore, right ? – Valentin O. Oct 06 '16 at 10:08
  • When your start your explorer with start-process you can use code like this: – guiwhatsthat Oct 06 '16 at 10:10
  • $process = Start-Process explorer.exe -PassThru $ProcessActive = Get-Process -Id $process.Id -ErrorAction SilentlyContinue if($ProcessActive -eq $null) { #Disconnect } – guiwhatsthat Oct 06 '16 at 10:10
  • 1
    @guiwhatsthat - It's not so easy, running `Start-Process explorer.exe` doesn't create a new process, it just opens a new window of the existing `explorer.exe` process. Explorer is special in this way. And the explorer process never goes away until the user logs off. – Peter Hahndorf Oct 06 '16 at 11:18
  • @PeterHahndorf in this case, I just write my script to mape the drive, display the folder I'm interested in, and when the user is done, he logs off (what he should normally do anyway) and it will dismape the drive ? – Valentin O. Oct 06 '16 at 11:42