New-TemporaryFile not available when calling powershell.exe from PowerShell 7 through cmd.exe or CreateProcess
See original GitHub issueSteps to reproduce
PS C:\test> cmd.exe /c "powershell.exe -command ""New-TemporaryFile"""
Expected behavior
Command to success and return a temporary file path
Actual behavior
Error of "The term 'New-TemporaryFile' is not recognized as the name of a cmdlet, function, script file..."
Environment data
PowerShell 7.1 on Windows 10 x64 version 2004 (for pwsh.exe)
Windows PowerShell 5.1 (for powershell.exe)
Note
When callingPS C:\test> powershell.exe -command "New-TemporaryFile" the command works as expected. It seems that when passing through cmd.exe or ::CreateProcess() the Microsoft.PowerShell.Utility assembly somehow is loaded differently and a few commands, including New-TemporaryFile end up missing.
New-TemporaryFile is available both on PowerShell 7 and on Windows PowerShell 5.1 so this is quite an interesting behavior
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
New-TemporaryFile not available when calling powershell.exe ...
New-TemporaryFile not available when calling powershell.exe from PowerShell 7 through cmd.exe or CreateProcess #14469. Sign in to view logs.
Read more >New-TemporaryFile (Microsoft.PowerShell.Utility)
This cmdlet creates temporary files that you can use in scripts. The New-TemporaryFile cmdlet creates an empty file that has the .tmp file...
Read more >Cannot run script in Powershell or cmd - Microsoft Q&A
When I type restore.cmd either in Powershell or windows cmd I get error: 'PowerShell' is not recognized as an internal or external command,...
Read more >Running Powershell - SysPython - WordPress.com
So just save the command(s) to execute into a temporary file and then call powershell.exe with the above options and the file name...
Read more >Borrowing a built-in PowerShell command to create ...
Within PowerShell there is a built in Cmdlet called New-TemporaryFile . Running this cmdlet simply creates a random 0 byte file in the ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The problem is that Powershell V7 (pwsh.exe) adds “c:\program files\powershell\7\Modules” to PSModulePath and then Powershell V5 (powershell.exe) inherits the modified PSModulePath . I don’t know the reason that New-TemporaryFile has a problem with that when other commands do not. The best solution is to call pwsh.exe instead of powershell.exe. Or, if you’re not sure what version of powershell is making the call, use $((Get-Process -Id $PID).Processname) .
Workaround :
cmd.exe /c "powershell.exe -command ""Import-Module Microsoft.PowerShell.Utility; New-TemporaryFile"""