r/PowerShell • u/LordLoss01 • Mar 15 '26
Powershell script that acts as powershell when called?
Yeah, I know the title is confusing. I have a system where I can only run PowerShell scripts. I cannot run individual commands themselves, only scripts. It is an actual terminal.
However, it allows you to run it with a parameter. I've kind of managed to get working by doing the below:
param(
[Parameter(Mandatory = $true)]
[string]$Command
)
Powershell.exe "$Command"So I would do run PowerShellScript.ps1 -parameters Get-Process. This works.
Problem is, as soon as there's a space in the parameter, it fails, thinking it's a separate parameter. So I can't do run PowerShellScript.ps1 -parameters Get-process | where processname -like "*Teams*". Any advice on how to get around this? The terminal I have is very basic, trust me when I tell you it can't do much. The solution has to lie within the script itself.
7
u/Icolan Mar 16 '26
What is preventing you from running an interactive session but allowing you to run scripts? That seems like a senseless restriction.
3
u/ITGuyfromIA Mar 15 '26
Can you start an interactive powershell from your script?
Edit: Also, do your scripts actually run as the user you’re logged in as? (Or a service account?
24
u/CookinTendies5864 Mar 15 '26
Change Powershell.exe “$Command”
to
Invoke-expression $Command
.\myscript.ps1 -command 'get-process | where processname -like teams'
8
u/Accomplished_Cold665 Mar 16 '26
for an interactive shell you can try:
Powershell.exe -WorkingDirectory ~ -NoExit
Or:
Powershell.exe -NoExit -Command "powershell.exe"
If not, and you're still stuck running scripts and looking for a way around spaces, There's a method called arg 'splattting' where you define everything in $args, and when you make the call it's with an '@' -
so @args instead of $args. but read about it first. there's some gotchas and edge cases to take into account:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.5
also, do you know the version of powershell?
powershell.exe $PSVersionTable.PSVersion
7
u/gregortroll Mar 16 '26
Maybe have a chat with your InfoSec folks to work toward a solution that maintains security while letting you perform necessary functions, without hacking around the security controls?
4
u/justaguyonthebus Mar 16 '26
Maybe use $args instead of having a param block.
3
u/Owlstorm Mar 16 '26
If for some cursed reason invoke-expression doesn't work, calling Powershell.exe with EncodedCommand or File probably will.
2
u/BlackV Mar 16 '26
This seems odd
How do you run a script on the first place of you have to run every as a script?
That aside sounds like you are just missing your proper parameters
If it's an existing script
2
u/mrbiggbrain Mar 16 '26
You can interact with the script and give input via stdin correct? Would this work?
We are just reading in a string, converting it to a script block, and then invoking that script block. Downside, it's a very simple shell so it only supports one-line commands. No line breaks.
1
u/SaltDeception Mar 15 '26 edited Mar 16 '26
What happens if you run (with enclosing quotes and backticks)
Also, can you use
Invoke-Expressioninstead ofpowershell.exein your script?1
u/jimb2 Mar 15 '26
$args is a built-in array of the arguments passed, so you can use:
$command = $args -join ' 'You can also quote your command.
You can do the same with a named function argument (an array of strings) but $args works. It's kinda bad practice, but ok for a simple personal function. You don't need to specify a function parameter at all, $args is always there.
I do this join trick for a bunch of shorthand command line utility functions - eg get group members - where I want to specify a string with spaces but don't want to type the quotes 20 times a day.
Note that running Powershell.exe like this will load a new PS environment and run the profile. You could potentially speed things with the -noprofile flag. Alternately, you could dot-run your command if it ok/useful to run in the parent environment, whatever that is.
1
u/Accomplished_Cold665 Mar 16 '26
PowerShellScript.ps1 -parameters 'Get-process | where processname -like "*Teams*" '
1
u/teethingrooster Mar 16 '26
Yea I’d try to remove the restriction if you can.
If you can’t maybe you can have an vscode up in ise mode write what you need save and run each time? I think there’s a hotkey to just run the script and you can set vscode to auto save to help speed up command entry
1
u/purplemonkeymad Mar 16 '26
For live response would you not just pull the forensics package? ie the collect command?
Your example can be done with the built in processes command.
I would have thought you would want to have your scripts be specific re-mediations, ie
Then you would use "run stopallteams.ps1" to do that action.
1
u/icepyrox Mar 16 '26
This is silly and seems like a work around to a problem that needs a better solution, but you might get away with making the parameter a [scriptblock] and putting it in {}
Or toss it in a text file (e.g., script.txt) and encode it.. ([convert]::toBase64String([text.encoding]::unicode.getbytes((get-content script.txt))) and pass that to -encodedCommand rather than -command
1
u/Accomplished_Cold665 Mar 17 '26
For those who aren't familar; I pulled this togehter from a few sources, so a few concepts are repeated.
Microsoft Defender Live Response is a capability within Microsoft Defender for Endpoint that gives security administrators remote interface access to a compromised or suspicious device.
Think of it as a secure, remote command-line shell (PowerShell for Windows, Bash for Linux/macOS) that allows you to perform forensic investigations and immediate remediation without being physically present at the machine or using traditional RDP.
Core Capabilities
How it works
Once the session is established in the Microsoft Defender portal, you have a command line where you can:
dir,get-process,get-service, orcatto inspect the file system and running state..ps1files that have been uploaded to the Library. This is the most common way to perform complex logic.putto move a tool (like a specialized scanner) onto the machine andgetto pull a suspicious file off for analysis.Unlike a local shell, you cannot simply copy-paste a 500-line script into the console. For security and auditing:
run script.ps1).Key limitations: