How to perform Win+D or Win+M with confidence? I mean, without using unreliable SendKeys and with return value. Nothing unusual, as there is the ‘Show desktop’ button.
Task: click on the ‘Show desktop’ button programmatically.
Requirements: use UIAutomation module to minimize all the windows without an exception.
Solution: there would be suggested a traditional way:
ipmo [path]\UIAutomation.dll; Get-UIADesktop | Get-UIAButton -Name 'Show desktop' | Invoke-UIAButtonClick;
Nope, this does not work even if we are running the PowerShell console window in the Minimized window state.
How can we fix the issue? Let’s use a Win32 click:
ipmo [path]\UIAutomation.dll; Get-UIADesktop | Get-UIAButton -Name 'Show desktop' | Invoke-UIAControlClick;
This code works as expected, from a minimized console, from a normal console, from everywhere.
It’s easily to activate a window that is in the Normal or Maximized window state. However, what to do if a window is minimized? There’s no problem if it’s seen in the Windows TaskBar.
Task: click the window out of Windows TaskBar.
Requirements: use UIAutomation to demostrate the technique.
Solution: below is the piece of code that does the same the user does when one needs to return the window to a visible state. Apart with Alt+Tab and Alt+Shift+Tab, there is the simple click on the application’s rectangle in TaskBar.:
ipmo [path]\UIAutomation.dll; # use the desktop element to get the button in TaskBar # desktop AutomationElement is nothing else but # [System.Windows.Automation.AutomationElement]::RootElement Get-UIADesktop | Get-UIAButton -Name 'Windows Media Player' | Invoke-UIAButtonClick;
After the code is processed by PowerShell, the window of your choice appears on the screen.