Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. # Toggle-PowerShell.ps1
  2. # Toggles focus between powershell and the current active window.
  3. # If this script isn't run with -NoProfile, it will switch focus to itself.
  4.  
  5. . $PSScriptRoot..FunctionsToggle-Window.ps1
  6.  
  7. Add-Type @"
  8. using System;
  9. using System.Runtime.InteropServices;
  10. public class Util {
  11. [DllImport("user32.dll")]
  12. public static extern IntPtr GetForegroundWindow();
  13. }
  14. "@
  15.  
  16. $a = [util]::GetForegroundWindow()
  17.  
  18. # Get-Unique may be unnecessary here, but including it for the case when
  19. # working with Chrome as the stored process
  20. $storedProcess=get-process | ? { $_.mainwindowhandle -eq $a } | Get-Unique
  21.  
  22. If(Test-Path $PSScriptRootToggle-PowerShell.temp)
  23. {
  24. $tempArray=(Get-Content $PSScriptRootToggle-Powershell.temp)
  25.  
  26. # the id number is at index three of tempArray
  27. Show-Process -Process (Get-Process -id $tempArray[3])
  28.  
  29. # delete the file so the next time we run the script it toggles to PS
  30. Remove-Item $PSScriptRootToggle-PowerShell.temp
  31. } Else
  32. {
  33. $propertiesFile=$PSScriptRoot..currentSession.properties
  34. $propertiesMap = convertfrom-stringdata (get-content $propertiesfile -raw)
  35.  
  36. Show-Process -Process (Get-Process -id $propertiesMap.'PowerShellPID')
  37.  
  38. # write a new temp file that contains the stored process's id
  39. # so that the next time this script is run it toggles back
  40. $storedProcess | Select-Object Id > $PSScriptRootToggle-PowerShell.temp
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement