Execute Batch in Powershell (Win 10) does not affect Parent Shell - powershell

just for understanding this.
I want to open my Powershell in a certain folder. As I didn´t find out how, I tried to put a batch file with just "cd ....." in it in the default folder where PowerShell opens.
When I execute the batch, though, I end up where I started from.
It seems that the batch gets excuted in a subshell which doesn´t affect the Parentshell.
How can I execute the stuff in the batchfile in parentshell ?
Thanks in advance!

You cannot. Batch files are executed by cmd, not PowerShell, so there will always be a new process for them.
With a PowerShell script you can use dot-sourcing
. Script.ps1
To execute the script in your current scope, which is most similar to how batch files are executed by cmd by default.

If you want to open your Powershell in a certain folder, you can set that up in your Powershell profile. In Powershell, type $profile and that will give you the location of your profile file. Edit that file and use Set-Location:
Set-Location 'C:\Some\Place'
Powershell will execute whatever is in your profile script every time you open a new Powershell session.

Related

How to run an executable through PowerShell without batch files or cmd?

I am currently using batch files to run a set of simulations. Each line in the batch file reads:
"filepath\program.exe" "filepath\simulation.file"
The quotation marks exist to bound any spaces that exist within the file paths. Without any spaces in the file paths, the quotation marks can be removed.
When I run the batch file through PowerShell, using the following command, it works fine:
.\batch.bat
The executable is run and the output is written to the host, as if I was running the same batch file in cmd.
However, I want to ditch the batch files and run the command directly through PowerShell. When I run the following, I get the program to execute, though it doesn't run properly and I don't get anything written to host. It also appears to hang until I use Ctrl+C to cancel.
& "filepath\program.exe" "filepath\simulation.file"
Could you please help me with the following?
Any resources discussing how PowerShell executes batch files.
How to run an executable through PowerShell without using cmd or a batch file and have it write to host.
Have you tried Start-Process?
Start-Process -FilePath "filepath\program.exe" -ArgumentList "filepath\simulation.file" -Wait
PS: The Parameter -Wait is optional.
Although I'm not quite sure why yet, I found out that this issue only occurred while working remotely. Whenever I was connected to the network locally the command ran just fine.
Since I plan to execute the command on PCs that are situated locally on the network, I'll leave it at this for now.
Thanks to everyone who commented!

Running a small WMI Powershell Script

I'm trying to have a few scripts that I can map to run from my keyboard for quickly changing the monitor/screen brightness. After some searching on the internet, I found this script which works when I enter it into Powershell.
$monitor=#(gwmi WmiMonitorBrightnessMethods -ns root/wmi)[0]
$monitor.WmiSetBrightness(50,0)
After I saved it as a .ps1 file and tried running it from the file, powershell tells me: The term "path of the file" is not recognized as the name of a cmdlet, function... and so on.
I'm not familiar with Powershell at all, can someone help with what I need to add in order for the script to run properly?
By default you can't run a PowerShell script that is in the current directory without putting .\ in front of the script name, or calling the full path of the script.
This is a security feature.
If you are in the directory that contains the script, run it by executing in a PowerShell window:
.\yourscript.ps1
Where yourscript is the name of your script.
See here for more information: https://ss64.com/ps/syntax-run.html
You may also see this error if your script has spaces in its name. If that is the case, enclose the path in quotes:
.\'your script.ps1'

Execute any file as powershell script

I encountered a challenge that I failed to resolve the way I wanted it to do.
I got a file that contains a powershell script, but that file does not have the extension assigned to powershell. The question is: How can I execute a powershell in a script file with the wrong file extension (or none)?
Invoke-Expression does not seem to work because it always executes the default action assigned to the file type. If I give that cmdlet a *.txt file the editor pops open.
I know that I can resolve that by renaming the script file or naming it properly in the first place. This is what I ended up doing.
Still I wonder if it is possible to execute a file as a script with the wrong file extension without modifying, renaming or coping the file. And if it is not working… why is that?
Powershell is designed such that executing or dot sourcing a file requires a .ps1 extension, and Powershell.exe will refuse to run any file that doesn't have that extension.
One way to invoke Powershell code from a non-ps1 file is to launch Powershell.exe using STDIN, and pipe your script to it. This requires a new shell, so is not very good for launching scripts from within an existing scripting environment.
Powershell.exe - < thescript.txt
Another way is to create a temporary .ps1 file and execute that. This has the advantage of using the current scripting environment, but requires a temporary file.
Copy-Item -Path '.\thescript.txt' -Dest '.\temp.ps1'
. .\temp.ps1
del .\temp.ps1
In my opinion, the file extension restriction is silly, but that's how it was designed. Apocryphally, this is for security reasons, but I can find no citation to back it up.
Or you use Get-Content to read the file and then invoke that with Invoke-Expression or Invoke-Command -ScriptBlock.

Run a remote powershell script from a batch file

I want to have my batch file run a remote powerhell script.
I have Powershell \ip\Myscript.ps1
But it asks me for do i want to run this remote script.
I want to bypass this. with a -confirm:$false
How can i use that in a batch.
You got a few options here.
Copy the .ps1 file to a local directory and then call it via
Powershell. An ugly hack, but sometimes necessary if Execution Policy must not be changed.
Sign the .ps1 file. A huge pain in the backside.
Tell Powershell not to worry about Execytion Policy. The easy and dangerous way: powershell -executionpolicy unrestricted \\server\share\someScript.ps1.

Unable to load .ps1 powershell script from Powershell command (console)

I am stuck in this weird problem where I am trying to execute a powershell script from the powershell command prompt. But neither do I get any errors nor the script is loaded.
I have script in C:\temp\myFunction.ps1 (which has a method getMyName() )
I open the powershell command and navigate to this directory and execute
./myFunction.ps1
then there are no errors and return back to the next line in the prompt. But when I try to call the function getMyName - I get error getMyName is not recognised.
I have set the Execution-Policy to Unrestricted, I am running the powershell as Administrator
Try dot sourcing your script:
. .\myFunction.ps1
It's basic problem of Powershell script. Set the Path where you physically saved your file and then execute the Powershell script. One more thing
1. start your command window run as admin.
2. set the Powershell script policy for execution.