PowerShell Script call out from another PowerShell Script - powershell

I'm trying to call a PowerShell script from another PowerShell script. The script I'm trying to call is in a subfolder named "Release" of the current directory.
I tried the following command but get an 'file not found' error from the PowerShell console.
."..\Release\out-datatable.ps1"

I have solved this by simply change to the subdirectory.
I wrote the following command to my script
cd Release
. .\out-datatable.ps1

Related

How to run powershell script file from JScript?

I am trying to run powershell script from JScript.
powershell will pop up but the script is not getting executed.
Below is the sample code
var WSH = new ActiveXObject("WScript.Shell");
var exec= WSH.Exec("powershell .\test1.ps1");
exec.StdIn.Close();
test1.ps1 is a simple script which creates textfile.
Try putting the full path to c:\path\to\test1.ps1. I guess there is a different idea of "the current directory" which .\ is not finding the right file. Also try putting -NoExit on the end of the powershell command line, and maybe the window will stay open and you can read any error message.

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

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.

Command prompt not executing powershell scripts

I tried running a build today and found that my command prompt is not able to execute powershell scripts. To confirm this, I created a test powershell script which simply writes to console. The script works fine when run from powershell, but is not recognizable by the command prompt. Screenshots below.
test script working from powershell
test script not working from command prompt
Any suggestions/explantion as to why this is happening?
Resolved it. I created a profile.ps1 script under ..\Documents\WindowsPowerShell folder to load custom settings for powershell. Removing profile.ps1 from the folder or adding a -NoProfile switch parameter solves this.

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'

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.