Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Write and Run PowerShell Script Without Scripting

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about writing and running a Windows PowerShell script without scripting.

Hey, Scripting Guy! Question Hey, Scripting Guy! I like using the Windows PowerShell ISE, but I do not have rights to run a script. I am stuck using a twenty year-old command prompt with Windows PowerShell inside. What’s up with that?

—DM

Hey, Scripting Guy! Answer Hello DM,

Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I am sipping a cup of Gunpowder green tea, with a bit of fresh lemon juice in it. It is a nice refreshing treat for a midafternoon slump. Of course, the biscotti also helps. Anyway...

DM, welcome to Script Without Scripting Week. Because you use the Windows PowerShell ISE, does not necessarily mean that you are scripting. In fact, you can write a script and run a script, and still not be scripting.

I have pretty much quit my exclusive use of the Windows PowerShell console, and I do nearly all of my Windows PowerShell work inside the Windows PowerShell ISE. But unless I am doing something complex, I do not bother writing a script. I just use the Windows PowerShell ISE as a better, more modern console. If I choose to write in the upper-script pane or type directly in the interactive pane is more a function of whether my command will be one line or longer.

Even if have your script execution policy set to Restricted, you can still use the Windows PowerShell ISE. For Windows PowerShell purposes, a script is not a script until it reads from the disk. So all commands typed in the script pane and executed are simply Windows PowerShell commands, and they are not subject to the script execution policy. However, when you save the code to a file, the next time you run it, it will be a script. Here is an example:

Image of command output

When I save the script as a file, and then I attempt to run the script, the following error message appears to inform me that running scripts is disabled:

Image of error message

If I open the script in Notepad, copy the contents to the clipboard, and then paste the script into a new Windows PowerShell ISE pane, I can run the script as I did before. This is shown here:

Image of command output

Using bypass

There are many ways to run a Windows PowerShell script when the execution policy is set to Restricted. But the easiest way is to launch Windows PowerShell with the bypass execution policy. Here is the command:

powershell -executionpolicy bypass

As shown here, I type this command in the Run dialog box:

Image of dialog box

This command launches the Windows PowerShell console in bypass mode. But what about the Windows PowerShell ISE? Let's try to use the following command:

powershell_ise -executionpolicy bypass

I receive an error message that says there is no such parameter:

Image of error message

So what do I do? As shown here, I can use the simple ISE command inside the Windows PowerShell console that I launched in bypass mode:

Image of command

Now I go to my newly opened Windows PowerShell ISE, and I see that it is in bypass mode. I then open and run my Windows PowerShell script:

Image of command output

DM, that is all there is to using the ISE without scripting. Script Without Scripting Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>