How to right click on a file in the system and create a shortcut on the desktop

Aug 8, 2013 at 5:21 PM
I have the following powershell code which I thought would work for this purpose but I have no Idea why it doesn't work.
Get-UIAActiveWindow  | `
Get-UIAListItem -Name $fileName | `
Invoke-UIAListItemSelectItem -ItemName $fileName | `
Get-UIASelectedItem  | `
Invoke-UIAControlContextMenu | `
Get-UIAMenuItem -Name 'Send to' | `
Invoke-UIAMenuItemExpand | `
Get-UIAMenuItem -Name 'Desktop (create shortcut)' | `
Invoke-UIAMenuItemClick
Could someone please point out where I am going wrong?

Thanks!!
Coordinator
Aug 13, 2013 at 5:11 AM
Edited Aug 13, 2013 at 10:47 AM
Hi shadowscar,
Windows Explorer's menu and other controls are usually specific things. Even though they obey physic laws, they often disobey rules of UIAutomation. The "Send to" menu item offers ExpandPattern, however I found only one way to expand it: put the cursor over. The "Desktop (create shortcut)" menu item must be clickable via IInvokePattern, nonetheless it doesn't. I tried Invoke-UIAMenuItemClick, Invoke-UIAcontrolClick, Set-UIAControlKeys and Set-UIAControlText cmdlet, they all did nothing.
Finally, my code that works on Windows 8 x64 En (UIAutomation 0.8.6B5+) is:
[UIAutomation.Preferences]::Timeout = 10000; # The appearance of the "Desktop" menu item may take more time
# file "test.txt" in the fiolder "11111", file extensions are hidden by default, folders are running in another process (Explorer setting)
Get-UIAWindow -pn explorer -Name 11111 | `
Get-UIAListItem -n test | `
Invoke-UIAControlContextMenu | `
Get-UIAMenuItem -n *send*to* | `
Move-UIACursor -X 10 -Y 10 | `
Get-UIAMenuItem -Name *desktop* | `
Invoke-UIACustomClick;
The code works for me, even throwing an error on the last call. We'll investigate into the error soon, this should be something like "InvokePattern probably failed and the supporting code in behind produces a Win32 click".

Upd: I tried this code on another Win 8 x64 in a domain, with a number of application running - the code worked not every time...
Aug 13, 2013 at 1:45 PM
Thanks Xinliu!!! You are awesome!!! Your post helped point me in the right direction. I used a different version of the code and it seems to be working for me now. But the catch is that I should have selected a file in the folder which I achieved through SendKeys. Below code worked for me
    Get-UIAWindow -Name $folderName | `
    Get-UIAListItem -Name $fileName | `
    Invoke-UIAControlContextMenu | `
    Get-UIAMenuItem -Name 'Send to' | `
    Invoke-UIAMenuItemExpand | `
    Get-UIAMenuItem -Name 'Desktop (create shortcut)'  | `
    Invoke-UIAMenuItemClick; 
Hope this helps others!!

PS: I am using Windows 7