programming4us
         
 
 
Windows

Windows 7 : Scripting Windows with PowerShell - Creating PowerShell Scripts

- Windows 10 Product Activation Keys Free 2019 (All Versions)
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
5/4/2011 6:25:59 PM
Running individual command at the PowerShell console is fine, but if you have commands you run regularly, or if you want to combine multiple commands into a single package, you need to create PowerShell scripts.

Setting the Script Execution Policy

By default, PowerShell is configured to not allow any unsigned scripts to run. That’s a sensible precaution on systems that don’t have a PowerShell programmer, but that’s not the case on your system. So, you need to configure PowerShell to allow your own scripts to run, and you do that by changing the PowerShell execution policy.

However, only the system Administrator account can change the PowerShell execution policy. That is, it’s not enough that your user account is a member of the Administrators group: It must be the Administrator account itself. Because you change the execution policy at the PowerShell prompt, you need to open a new PowerShell using the Administrator account:

1.
Select Start.

2.
Type powershell.

3.
Right-click Windows PowerShell in the search results, and then click Run as Administrator. The User Account Control dialog box appears.

4.
Enter your UAC credentials to continue. The Administrator: Windows PowerShell window appears.

With your Administrator PowerShell session loaded, enter the following command at the PowerShell prompt:

set-executionpolicy remotesigned

PowerShell displays the following message:

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing
the execution policy might expose you to the security risks described in the
about_Execution_Policies help topic. Do you want to change the execution policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):


Type y and press Enter (or just press Enter) to put the new policy into effect. You can now run your own scripts.

Working with the PowerShell Integrated Scripting Environment

Although you could easily use Notepad or some other text editor to create your scripts, you’re much better off using the PowerShell Integrated Scripting Environment (ISE), which includes programmer-friendly goodies such as color-coded syntax, breakpoints, stepping through scripts, and more.

To launch PowerShell ISE, select Start, type power, and then select PowerShell ISE in the search results. Figure 1 shows PowerShell ISE with a script loaded.

Figure 1. PowerShell ISE provides a powerful and efficient place to build your scripts.

The PowerShell ISE window is divided into three panes:

  • Script pane— This is the top pane, and it’s where you type your script code. PowerShell ISE automatically color-codes your text to make it easier to read the code and to help you find errors. For example, cmdlets appear in blue; variables appear in red; comments appear in dark green; and strings appear in dark red.

  • Command pane— This is the bottom pane, and it works pretty much like the PowerShell console. That is, you can use the prompt top enter PowerShell commands, which saves you having to open a separate PowerShell command-line session.

  • Output pane— This is the middle pane, and it’s where PowerShell ISE displays the output from your script or from the commands you enter into the Command pane.

Tip

You can switch from one pane to the other using keyboard shortcuts: Press Ctrl+I to switch to the Script pane; press Ctrl+D to switch to the command pane; and press Shift+Ctrl+O to switch to the Output pane.


Running PowerShell Scripts

If you’re using the PowerShell ISE to compose your scripts, you don’t have to leave the application to run the current script. Instead, either click the Run button in the toolbar or press F5. PowerShell ISE runs that script within the window, and if your script has output, it’s displayed in the Output pane (the middle pane).

If you’re running a script from the PowerShell command line, you might think you can change to the folder where your script resides (using the command cd path, where path is the full pathname of the folder), type the name of the script, and then press Enter. That would be nice, but it isn’t likely to work. That’s because PowerShell is configured to run scripts only under the following circumstances:

  • If the script is stored in a folder that’s part of your system’s Path environment variable

  • If you specify the full pathname of the script

If you only run the occasional script and your PowerShell session is currently in your scripts folder, PowerShell offers a shortcut method for specifying the full pathname of the script. If you precede the script filename with a dot (.), PowerShell interprets that dot as the parent folder of the current folder, so it’s just the same as typing out the entire path.

For example, if you have a script named RegAdd.ps1 stored in the Documents\Scripts folder, you’d normally have to enter the following to run the script (where account is your user account name):

c:\users\account\documents\scripts\regadd.ps1

However, if you’ve got PowerShell in the Scripts folder, you can run the same script using the following command:

.\regadd.ps1

On the other hand, if you run lots of scripts, or if you want to be able to run your scripts from any folder, you need to add your Scripts folder to the Path variable. Here’s how it’s done:

1.
Select Start, type systempropertiesadvanced, and then press Enter. Windows 7 displays the System Properties dialog box with the Advanced tab displayed.

2.
Click Environment Variables. Windows 7 opens the Environment Variables dialog box.

3.
In the System Variables list, select Path, and then click Edit. The Edit System Variable dialog box appears.

4.
In the Variable Value text box, move the cursor to the end of the existing value, type a semicolon (;), and then type the full path to the folder where you store your scripts. Figure 2 shows an example.



Figure 2. Add your script folder to the Path environment variable to run your scripts from any folder.


5.
Click OK in all open dialog boxes.

6.
If you have a PowerShell session running, shut it, and then restart PowerShell to put the new Path variable into effect.
Other -----------------
- Windows 7 : Scripting Windows with PowerShell - Scripting Objects
- Windows 7 : Scripting Windows with PowerShell - Running PowerShell Cmdlets
- Windows 7 : Scripting Windows with PowerShell - Getting Started with PowerShell
- Scripting Windows 7 with WSH : Programming the Windows Management Instrumentation Service
- Scripting Windows 7 with WSH : Scripting Internet Explorer
- Scripting Windows 7 with WSH : Programming the WshNetwork Object
- Scripting Windows 7 with WSH : Programming the WshShell Object (part 2)
- Scripting Windows 7 with WSH : Programming the WshShell Object (part 1)
- Scripting Windows 7 with WSH : Programming the WScript Object
- Scripting Windows 7 with WSH : Programming Objects
- Scripting Windows 7 with WSH : Scripts and Script Execution
- Adding Macs to Your Windows 7 Network : Letting Windows Computers See Your Mac Shares
- Adding Macs to Your Windows 7 Network : Using a Mac to Make a Remote Desktop Connection to Windows 7
- Adding Macs to Your Windows 7 Network : Connecting to a Windows Shared Folder
- Adding Macs to Your Windows 7 Network : Connecting to the Windows Network
- Windows 7 : Controlling and Customizing Your Website (part 5) - Viewing the Server Logs
- Windows 7 : Controlling and Customizing Your Website (part 4) - Disabling Anonymous Access
- Windows 7 : Controlling and Customizing Your Website (part 3) - Working Without a Default Document
- Windows 7 : Controlling and Customizing Your Website (part 2) - Setting the Website’s Default Document
- Windows 7 : Controlling and Customizing Your Website (part 1)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS