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.
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