.ps1 cannot be loaded because the execution of scripts is disabled on this system

I run this code to execute PowerShell code from an ASP.NET application: System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); runspace.Open(); System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(@”\\servername\path”); pipeline.Commands.Add(“Out-String”); Collection<PSObject> results = pipeline.Invoke(); runspace.Close(); But I am getting an error: .ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details. … Read more

How to get the current directory of the cmdlet being executed

This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute C:\temp\myscripts\mycmdlet.ps1 which has a settings file at C:\temp\myscripts\settings.xml I would like to be able to store C:\temp\myscripts in a variable … Read more

How to run a PowerShell script from a batch file

I am trying to run this script in PowerShell. I have saved the below script as ps.ps1 on my desktop. $query = “SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2” Register-WMIEvent -Query $query -Action { invoke-item “C:\Program Files\abc.exe”} I have made a batch script to run this PowerShell script @echo off Powershell.exe set-executionpolicy remotesigned -File … Read more

How to tell PowerShell to wait for each command to end before starting the next?

I have a PowerShell 1.0 script to just open a bunch of applications. The first is a virtual machine and the others are development applications. I want the virtual machine to finish booting before the rest of the applications are opened. In bash I could just say “cmd1 && cmd2” This is what I’ve got… … Read more