I'm on Powershell 5.1 which IIRC supports ThreadJob OOB. I have a script which uses the module which used to work without issues. Now I'm getting the following error:
ForEach-Object : The term 'Start-ThreadJob' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
Admittedly I haven't run the script (or used ThreadJob) in a while, but I get the "not recognized" error even when I just call Start-ThreadJob
from the terminal.
I've rebooted and run DISM and sfc which didn't find any issues.
This is on a fairly fresh Windows 10 install.
Am I forgetting something? I vaguely recall experiencing something like this a long time ago, but don't remember how I got around it.
CodePudding user response:
Windows PowerShell - the legacy, Windows-only, ships-with-Windows edition of PowerShell whose latest and last version is 5.1.x - does not come with the ThreadJob
module that provides the Start-ThreadJob
cmdlet - only the modern, cross-platform, install-on-demand PowerShell (Core) edition (v6 ) does.
However, you can install the module on demand in Windows PowerShell, from the PowerShell Gallery, with Install-Module -Scope CurrentUser ThreadJob
, for instance.
The following cross-edition idiom installs the module on demand when running in Windows PowerShell, in the scope of the current user:
if ($PSVersionTable.PSVersion -lt 6 -and -not (Get-Command -ErrorAction Ignore -Type Cmdlet Start-ThreadJob)) {
Write-Verbose "Installing module 'ThreadJob' on demand..."
Install-Module -ErrorAction Stop -Scope CurrentUser ThreadJob
}
# Getting here means that Start-ThreadJob is now available.
Get-Command -Type Cmdlet Start-ThreadJob