Execute any file as powershell script -


i encountered challenge failed resolve way wanted do.

i got file contains powershell script, file not have extension assigned powershell. question is: how can execute powershell in script file wrong file extension (or none)?

invoke-expression not seem work because executes default action assigned file type. if give cmdlet *.txt file editor pops open.

i know can resolve renaming script file or naming in first place. ended doing.

still wonder if possible execute file script wrong file extension without modifying, renaming or coping file. , if not working… why that?

powershell designed such executing or dot sourcing file requires .ps1 extension, , powershell.exe refuse run file doesn't have extension.

one way invoke powershell code non-ps1 file launch powershell.exe using stdin, , pipe script it. requires new shell, not launching scripts within existing scripting environment.

powershell.exe - < thescript.txt 

another way create temporary .ps1 file , execute that. has advantage of using current scripting environment, requires temporary file.

copy-item -path '.\thescript.txt' -dest '.\temp.ps1' . .\temp.ps1 del .\temp.ps1 

in opinion, file extension restriction silly, that's how designed. apocryphally, security reasons, can find no citation up.


Comments