For a long time now, our admins have been creating desktop shortcuts using the WShell.Script Objects
# Create a Shortcut with Windows PowerShell $TargetFile = "$env:SystemRoot\System32\notepad.exe" $ShortcutFile = "$env:Public\Desktop\Notepad.lnk" $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile) $Shortcut.TargetPath = $TargetFile $Shortcut.Save()
Our developers (most of which are previous Java developers now using PowerShell) have been writing using TDD methodology and came across this scenario of creating desktop shortcuts. After some research on the topic, we’ve been unable to find a more testable (Powershell cmdlets) way to create the code. The question here is:
- What is a good way to make this testable?
- Maybe create a new mocking class within our tests?
- Maybe use New-MockObject but as of right now, we are unable to create it using a good type?
- Is there a PowerShell way to create a shortcut without using Wshell?
SideNote: All systems use Powershell 5.1.