question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New-TemporaryFile not available when calling powershell.exe from PowerShell 7 through cmd.exe or CreateProcess

See original GitHub issue

Steps 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:open
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
RobMahncommented, Mar 13, 2023

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) .

2reactions
GaetanCambiercommented, Oct 18, 2021

Workaround : cmd.exe /c "powershell.exe -command ""Import-Module Microsoft.PowerShell.Utility; New-TemporaryFile"""

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found