How to write and run PowerShell scripts. How to open PowerShell, all methods

1. Writing a script

A PowerShell script (no matter what version) is a text file with a *.ps1 extension.

Here is an example of a simple Power Shell script (file systemInfo.ps1):

# Retrieve WMI object for the operating system

Get-WmiObject Win32_OperatingSystem

This file can be created and edited, for example, in FAR Manager.

notethat FAR Manager, although it can work in the Power Shell console, executes scripts from under itself in the environment of a regular Windows console cmd . That is, FAR Manager can only be used to create and edit PowerShell scripts, but not to run them. But before you get disappointed, read point 3.

2. Running the script

The script must be executed from the Power Shell console, and not from the usual one Windows console. In the Power Shell console, you need to go to the directory where the script is located (with the commands CD ), and then run the script itself, making sure to include the characters in front of it".\" . For example, we have the path to the script file d:\work\systemInfo.ps1 . Then the launch commands will look like this:

cd\

cd work

.\systemInfo.ps1

or like this (just specify the full path to the script):

d:\work\systemInfo.ps1

Most likely, the following error will appear when running the script:

The file D:\work\systemInfo.ps1 cannot be loaded because script execution is not allowed on this system. Type "get-help about_signing" for more information.

line:1 character: 18

CategoryInfo: NotSpecified: (:), PSSecurityException

FullyQualifiedErrorId: RuntimeException

The error appears because by default Power Shell has the maximum security policy enabled, which allows you to run PowerShell commands on the command line, but does not allow you to run a script with PowerShell commands on the same command line.

To enable the execution of PowerShell scripts, you need to create a *.bat file, for example enableScript.bat with the following content:

powershell -Command Set-ExecutionPolicy RemoteSigned

This *.bat file can be executed in any console: either in PowerShell or in a regular console cmd . After executing this file, PowerShell scripts will run in the PowerShell console.

3. Running a PowerShell script from a regular Windows cmd console

The PowerShell script can also be executed from a regular Windows console. To do this you can use the command:

Powershell -File ./systemInfo.ps1

This way you can execute scripts directly from FAR Manager and they will work.

But there is a slight subtlety here. Parameter-File only fires on local paths, even if the path is relative"./" . That is, if *.ps1 - the file is on the local disk C: or D: , then such a call will work. But if you try to execute a script located on a domain resource, the script will not be found. Perhaps this will be fixed in future versions of PowerShell.

Hi all! Today a short note on the topic, how to open powershell as administrator. Let me remind you that PowerShell is a powerful programming and administration language from Microsoft, every year it grows, everything large quantity cmdlets and functionality. Essentially, it is a replacement for the Windows Command Prompt. Below we will consider methods that allow you to open and configure it.
.

Methods for opening a PowerShell snap-in

Powershell is developing very well and with the release of Windows 10 it has already received version 4, but our topic is different. So how to open powershell? Everything is simple if in Windows XP, then nothing. Since it is delivered separately, in all subsequent releases it comes as a built-in component. Most universal method open powershell this click

Win+R and enter powershell

By pressing enter, the PowerShell console will launch, the only problem is that it will not open as administrator. And as a result, many commands will not be executed; below we will see how to do this on behalf of the administrator.

How to open windows powershell as administrator in Windows 8.1 and Windows 7

You can open Windows Powershell through Start. In Windows 8.1, Windows 2012 R2, go to System Tools - Windows and right-click and select Run as administrator.

In Windows 7 and Windows 2008 R2 it looks like this Start > Accessories > Windows PowerShell

You can also create a shortcut in the task item and right-click on it and select the appropriate item

It would be convenient if PowerShell always opened as administrator, let's implement it. This is done the same way in all versions of Windows. Opening the control panel

Right-click on it and select Properties. You can immediately see the path to the file where it is located in the system.

Click the "Advanced" button. You will see additional properties. where you need to check the box Run as administrator.

Everything is very simple. I am sure that now you will not have a question about how to open Windows Powershell. Another useful thing is to change the font in the powershell window.

How to Open PowerShell from the Start Button Context Menu in Windows 10

Microsoft is increasingly placing emphasis from the command line towards its strong language (PowerShell), and this is logical, since its capabilities for managing operating systems Windows family, are almost limitless, if not more, than in the graphical interface. Beginning with Windows versions 10 1709, in the context menu, PowerShell, replaced the usual cmd for everyone. Right-click on the start button in Windows 10 and from the context menu, select the appropriate item:

  1. Windows PowerShell
  2. Windows PowerShell (administrator) is exactly the mode with maximum rights in Windows 10.

Launch Windows PowerShell using Search in Windows 10

On Windows 10 and older versions, you can find PowerShell using regular search, there is a special section for this. Click next to the Start button, the magnifying glass icon. In the search form that opens, enter the word PowerShell. You will search for all options, among which you will find the shell. If you right-click on it, you can open it as an administrator.

Launching Windows PowerShell from the Start menu in Windows 10

To open PowerShell from the Start menu, open it and find the Windows PowerShell item, it will be in the form of a folder, open it and run the appropriate version.

If you right-click, you can run the Vershel shell as an administrator with administrator rights.

Additional and generic shell launch methods

There are also very specific methods for opening a shell with a powerful language from Microsoft. The most clumsy one, it's easy to find executable file and run it directly from a location on the system. Let's go to your Windows 10 along the following path:

C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell

As a result, you will see a folder with shortcuts that are present in the Start menu, you can launch them.

You can also run the original executable file, which is located along the path:

C:\Windows\System32\WindowsPowerShell\v1.0

And by right-clicking on it, you can download it as an administrator, with maximum rights.

You can also launch Windows PowerShell from the command line window; to do this, simply enter the magic word in it and press Enter.

Well, let me also remind you of the method for launching a new task from the “Task Manager” snap-in. Open "Task Manager", select menu item "File - Run new task"

In the window that appears, enter PowerShell.

Well, the last method known to me is to launch PowerShell, via Windows Explorer 10, to do this, open Explorer, select the menu item "File - launch Windows PowerShell", there will be both modes.

What is PowerShell ISE

You've probably noticed that the PowerShell ISE shortcut is everywhere, and you'd like to know what it is. In a nutshell, this is a special shell in which you can write scripts and scenarios in PowerShell, using all the variety of cmdlets that Microsoft offers you.

Powershell changing the font is very simple, go to the Font tab in the properties, where you can set the size from 5 to 72.

You can also get to properties from the utility window itself, click on the icon in the upper left corner and select properties

Here the font settings are slightly different, and as they change, the size of the console also changes.

On the Colors tab, you can set the font color in Powershell and the window itself. By making it black, for example, like the command line.

I also advise that if you are an active console user, set the buffer size not to 50 commands, but at least to 100.

Thank you for your attention. These are the methods for opening the Power Shell console in Windows. Site material

Experienced operating room users Windows systems 10 may have heard of the PowerShell command line utility. Those with experience working with Windows 7 and 8.1 may also be familiar with it. After many years of using the command app Windows strings and .bat files, it's time to get acquainted with a more functional tool.

PowerShell is a valuable addition to the list Windows tools and its scale may deter some users. What is it - a scripting language or a command shell? Don't worry: despite the extensive capabilities, anyone can master PowerShell.

Step 1: Installation

First, we need the PowerShell utility itself. If you're running Windows 10, you already have PowerShell 5 installed. Windows Update The 10 Anniversary Update uses version 5.1, but the difference is not noticeable. Windows 8 and 8.1 use PowerShell 4, which is also quite sufficient. Installing PowerShell on Windows 7 will not cause problems, but you will still have to take a couple of extra steps. In particular, you need to additionally install the .Net Framework. You can install the Windows Management Framework, which includes PowerShell.

PowerShell has two interfaces. Advanced users can opt for a full GUI interface known as the Integrated Scripting Environment (ISE). For beginners, it's best to use the PowerShell console, a simple text-based Windows command line-style interface, or even DOS 3.2.

To run PowerShell as an administrator in Windows 10, click the Start button and scroll down to Windows PowerShell. On Windows 8.1, look for Windows PowerShell in the System folder in Windows. In Windows 7, the shell is located in the Accessories folder. As a regular user, PowerShell can be launched in exactly the same way using left button mouse instead of the right one.

You can use search on any version of Windows. For ease of future use, you can pin PowerShell to the taskbar.

Step 2: Old Windows Commands

The Windows command syntax in PowerShell works as usual. For example, CD changes folders, dir shows a list of all files and folders inside the current folder.

Depending on how you launch the PowerShell console, you might start in c:\Windows\system32 or in c :\Users\ . The example in the screenshot uses the command CD..(with a space) to move up one level at a time, then run the command dir to display a list of files and folders in a directory C:\.

Step 3: Install Help Files

Teams like CD And dir are not active PowerShell commands. These are so-called aliases - replacements for real PowerShell commands. Aliases are useful for those who have extensive experience with the command line. However, they don't touch the depths of PowerShell.

To get started with PowerShell, type help and the command you need. The screenshot shows the command .

Team help says that dir is an alias for the PowerShell command Get-ChildItem. If you type get-childitem V PS C:\>, you will see the same thing as when using the command dir.

As noted at the bottom of the screenshot, help files for PowerShell are not installed automatically. To get them, run PowerShell as administrator and type update-help. Installing the help files will take several minutes; a number of modules may be missing - for example, in this case, Help for NetWNV and SecureBoot were not installed. When everything is ready, the complete help system will always provide the necessary tips.

Now type the command get-help and any command you are interested in (“cmdlet” in PowerShell, cmdlets in Russian), its description will be shown. For example, get-help get-childitem displays a list of options get-childitem. You can also display different possible options. For example

get-help get-childitem -examples

provides seven detailed usage examples get-childitem. Team

get-help get-childitem -detailed

includes these seven examples and detailed explanations of each parameter in the cmdlet get-childitem.

Step 4: Get help with settings

In the screenshot you may have noticed two lists under SYNTAX For get-childitem. Having two different syntaxes means there are two ways to run the cmdlet. How to keep the syntax separate from each other and what do the parameters mean? The answer is simple if you know the trick.

For details regarding cmdlet parameters get-childitem or any other use parameter -full:

get-help get-childitem -full

This gives you a list of what you can do with the cmdlet and what will happen. Look at the screenshot.

Reviewing the parameter descriptions, you will notice that get-childitem makes it possible to get an object child(such as a subfolder name or file name) in a specified location, with or without matching certain characters. For example:

get-childItem “*.txt” -recurse

returns a list of "*.txt" files in the current folder and all subfolders (due to the parameter -recurse). Whereas

get-childitem “HKLM:\Software”

returns a list of all top-level registry keys in HKEY_LOCAL_MACHINE\Software.

If you've ever tried to get into the registry using the Windows command line or .bat files, you'll appreciate the functionality of this access option.

Step 5: Learning Names

There's a reason why the cmdlets shown so far look similar: get-childitem, update-help, get-help use a single verb-noun pattern. All PowerShell cmdlets use this convention; they use a verb before a single noun. This will appeal to those who once suffered from inconsistent command names in the VB and VBA languages.

Take a look at the most common cmdlets:

set-location: sets the current working location to a specific location

get-content: Gets the contents of the file

get-item: Retrieves files and folders

copy-item: copies an object from one location to another

remove-item: Deletes files and folders

: Gets processes running on a local or remote computer

get-service: Gets services running on a local or remote computer

invoke-webrequest: Retrieves content from a web page on the Internet

To view how a specific cmdlet works, use get-help as in the case

get-help copy-item -full

Based on the description in the help, you can understand what the cmdlet needs. For example, if you want to copy all files and folders from Documents V c:\temp, use

copy-item c:\users\ \documents\* c:\temp

By typing this command, you'll see several interesting features of the PowerShell environment. For example, if you type copy-i and press the Tab button, PowerShell will fill in Copy-Item. If you type a cmdlet incorrectly and PowerShell cannot recognize it, a full description of what was done wrong is given.

Try this cmdlet:

invoke-webrequest askwoody.com

You'll get a short list of the web page's titles, images, links, and other content. Please note in get-help to the list invoke-webrequest, which “returns a collection of forms, links, images and other important HTML elements" - exactly what should be shown on the screen.

Some cmdlets help you manage PowerShell itself:

get-command: list of all available cmdlets

get-verb: list of all available verbs

clear-host: Clear the host program screen

Different parameters allow you to reduce commands and narrow down the range of useful options. For example, to see a list of all cmdlets that work with Windows services, type

get-command *-service

All verbs available with the noun will be shown service. Here is their list:

Get-Service

New-Service

Restart-Service

Resume-Service

Set-Service

Start-Service

Stop-Service

Suspend-Service

You can combine these cmdlets with others.

Step 6: Using Pipes

If you are familiar with the Windows command line or batch files, then you know about redirection and pipes. Redirection (> symbol) and pipes (| symbol) take the result of an action and attach it to another location. For example, you can redirect the result of the command dir to a text file or pass the result of the command ping to the team find to filter interesting results like

dir > temp.txt

ping askwoody.com | find “packets” > temp2.txt

Here on the second team find searches for a string packets, taken from the address askwoody.com by the team ping and concatenates all matching lines into a file called temp2.txt.

The first of these commands works fine in PowerShell. To run the second command you would need something like

ping askwoody.com | select-string packets | out-file temp2.txt

Using redirection and pipes greatly expands the capabilities of the Windows command line: instead of endlessly scrolling down the screen to search for a text string, you can filter the Windows commands you need.

Powershell has support pipe, and it is not limited to text. PowerShell allows you to pass an entire object from one cmdlet to another, where the object is a combination of data (called properties) and actions (methods) that can use that data.

The tricky part comes when lining up the objects. The objects supplied by one cmdlet must match the type of objects accepted by the receiving cmdlet. Text is a very simple object type, so if you're working with text, aligning objects is a simple task. The remaining objects are not so basic.

How to understand this? Use cmdlet get-member. If you want to know what type of object a cmdlet is processing, run it through get-member. For example, if you are trying to understand the processes running on a computer and have narrowed your options down to cmdlets , here's how to find out the result of the cmdlets:

get-process | get-member

Running this cmdlet produces a long list of properties and methods for , but at the very beginning of the list you can see the type of object that creates :

TypeName: System.Diagnostics.Process

The below screenshot also shows the properties entitled get-process Handles, Name, NPM, PM, SI, VM And W.S..

If you want to manipulate the result To work with this cmdlet (instead of displaying a long list of active processes on the monitor), you need to find another command that takes as input System.Diagnostics.Process. To find the cmdlet you need, use PowerShell again:

get-command -Parametertype System.Diagnostics.Process

This cmdlet provides a list of cmdlets that can process System.Diagnostics.Process.

Some cmdlets are known for accepting almost any kind of data. Chief among them is . This cmdlet passes through each object sent through the pipe, one by one, and applies the specified selection criteria to it. There is a special marker called $_ , which allows you to use each item in the pipe, one at a time.

Let's say you want to get a list of all processes running on a computer with the name "svchost", that is, you want to match the property Name process svchost. Use the command:

get-process | where-object ($_.Name -eq “svchost”)

Cmdlet looks at every object System.Diagnostics.Process, compares .Name this object with "svchost"; if there are matches, they are displayed on the monitor. Look at the screenshot.

Step 7: Analyze Useful PowerShell Commands

By now you already know enough to be able to damage the computer, so be careful. Let's look, for example, at frequently requested PowerShell commands.

These commands only work on Windows 10 and only when you run PowerShell as an administrator. They are designed to reinstall pre-installed Windows applications 10 and may be useful to those who first deleted these programs and then decided to return them. The commands look like this:

Get-AppXPackage | Foreach (Add-AppxPackage -DisableDevelopmentMode -Register “$($_.InstallLocation)\AppXManifest.xml”)

If you run this command, ignore the red warnings and when the command completes, restart your computer; all pre-installed programs in Windows 10 will appear in their places.

Here's how this command works. Get-AppXPackage checks all application packages in the user profile. Even if you delete the application, it remains in the user profile list.

Cmdlet Get-AppXPackage returns an object TypeName Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage, which includes full name application package and the location of the corresponding XML manifest file. If you run the cmdlet get-appxpackage, you will see a long list of application packages. The screenshot shows the description of the Xbox app.

Cmdlet Foreach loops through each object in AppXPackage by sending them to the cmdlet Add-AppxPackage. According to get-help For Add-AppxPackage, there are two key switches:

  • Switch -Register used to register existing installations of application packages, you can set parameters DisableDevelopmentMode And Register
  • Switch -DisableDevelopmentMode tells Windows to re-register an existing application package that has been disabled, unregistered, or corrupted.

Line " $($_.InstallLocation)\AppXManifest.x ml" describes where the file is located manifest.xml. If you look at the files AppXManifest.xml, you'll see a complex list of application IDs, executable files, and a large number of visual elements associated with the application.

After the reboot, all added application packages are downloaded and installed from Windows Store Store.

PowerShell is an alternative command line with extensive functionality. Many computer owners who are well versed in system processes, are interested in the possibilities of using it on Windows 10 and previous versions this software to change specific processes and PC settings for yourself.

There is nothing complicated about using PowerShell. Just follow all the instructions in this article and you will succeed.

If you are not an expert in this topic, you are just starting to study command lines and the actions that can be performed in them, it will be useful for you to know the answers to the main questions about using such an application. For example, how to run Windows PowerShell ISE Windows 10 and write and work with scripts.

How to open PowerShell?

This can be done in several in the following ways:

  • - you will find the application in the general list of components under the letter W.
  • Using search in the Taskbar - open it and enter the word PowerShell to search.
  • Using the “Run” menu, run it through the Win + R combination and write the name of the utility, after which it will be found in the system.
  • In Commander, set the PowerShell action - but you should know that you will be working with the application through an already running line, and not in a separate window.
  • IN Windows settings Select Taskbar Properties and Start Menu. In the “Navigation” tab, check the box next to “Replace” command line PowerShell..." Now you can enable the utility using the Win+X combination.

How to write and run scripts?

How to create a PowerShell script and run it? This can simply be done in a notepad - using it, indicate the commands you need in the new document. Then save it, but change the format to .ps1 - this is the extension that the files of this system utility have.

You can write scripts in the application itself like this:

  • Launch it.
  • Click on the “Create” item.
  • Or click on “File” and select “Create” from the list that appears.
  • Write all the commands that need to be executed, save the file.

Scripts can still be written and edited in FAR Manager, but they cannot be used directly in such an application, because it displays files of this type through the standard cmd line. However, other actions are performed in the same way as in the “native” utility.

You can run the script this way:

  1. Log into PowerShell as an administrator.
  2. Use the cd commands to specify the location of the required file. For example, e: cd\ cd work .\ filename. Ps1, or e: \work\filename.ps1
  3. In this case e: is the name hard drive where the file is located.
  4. Press Enter.

The program will definitely show you a message with the text “The file ___ .Ps1 cannot be loaded because script execution is prohibited on this system...” when you run the command for the first time. The fact is that Windows is equipped with protection against interference thanks to the developers, so none of the users by default have access to perform such actions. But this limitation can be very easily circumvented.

How? The text that appears with the error will recommend that you enter the command get-help about_signing to get more information. You can follow these instructions, or use the Set-ExecutionPolicy remotesigned combination - after entering it, confirmation will be required, and the restriction will be removed.

Other questions about use

Many people have a question: how to find out the version of PowerShell? To do this, do the following:

  1. Enter $PSVersionTable.
  2. If this does not lead to anything, then you have version 1.0 - there is a $host.version command for it.
  3. Otherwise it will appear detailed information about what version of the application is installed on the PC.

More than one year has passed since the release of Windows 7 and Server 2008.

Programmers, system administrators and advanced users have found many uses for the blue window, similar to the command line.

Most novice users don’t even know what PowerShell (PS) is.

But scripts for PowerShell allow you to automate almost 100% of the actions and scripts performed in operating system via command line and graphical interface.

Let's get acquainted with the main capabilities of this program, paying more attention to the function of creating and running scripts (firmware).

There are countless manuals and documentation for the program, including in Russian. Purpose of the article– provide the user with conceptual knowledge, bring up to date, but whether it’s worth getting to know the interpreter and the execution of scripts in it more closely, everyone will decide for themselves.

What is Windows PowerShell?

PowerShell– an interpreter based on the .NET Framework, equipped with its own scripting language.

The first means that he works in text mode: you enter a command, run it, and on the screen you see the result of its execution.

As it was in MS-DOS and older versions of UNIX.

The second significantly simplifies, speeds up and automates the administration and maintenance of systems, applications, processes and related services by administrators and ordinary users.

Compared to other interpreters, PS is different:

  • integration with – allows you to create powerful scripts by introducing program code into them;
  • all returned data are objects, not text/string data, which means they are passed on to other scripts and processed in any way.

PowerShell version 2 has the following capabilities, Let's look at some of them in more detail:

  • Ability to represent commands as cmdlets– they are launched inside the interpreter; otherwise, the command is executed in a separate process.
  • Using Conveyors– are designed to transfer data from one command to another while maintaining their structure and type.
  • Integrated multi-stream data transfer over the network with prioritization and connection resumption.
  • Support for positional and named parameters.
  • Background work– asynchronous calling of commands and launching scripts on remote machines.
  • Setting up limited sessions with remote clients and running scripts on them.
  • Modules– a way of organizing scripts when they become self-sufficient and executed in their own container without affecting the module’s environment.
  • Availability of an error handler.
  • Graphical environment for the language: syntax, debugger, highlighting, automatic command completion with support for Unicode and bookmarks.
  • Adding breakpoints to lines, commands, operations and variables for debugging the script.
  • Block and interlinear comments.
  • Support for creating aliases for some cmdlets, which are converted into ordinary commands at the time of execution.
  • Creating limited sessions, where you can execute a strictly defined list of commands and much more.

PowerShell source code made available to everyone: Any community member can freely create their own extensions to increase the functionality of the command line interpreter.

You can start mastering an integrated scripting language without any programming skills.

It is unlikely that you will be able to create a complex scenario, but almost everyone will be able to perform sequences of primitive actions.

Although, without knowledge of the basic concepts of PowerShell, it is unlikely that you will be able to do anything in the command interpreter window. Let's start with them.

Cmdlets

Cmdlets– peculiar PS commands, behind which a variety of functions are hidden. The commands built into the interpreter are implemented on the verb-noun principle, for example, Get-Process (getting a list of processes). This solution allows you to understand the essence of the command just from its name (in English).

Some cmdlets support receiving/transferring data and arrays of information while preserving their structure and type. This works on the principle of a conveyor (this will be discussed in the next section). Regardless, the cmdlets run and process the objects in order.

To implement cmdlets, any supported .NET APIs created in any .NET language can be used.

Thus, the user is given access to specific functions of the program.

Cmdlets are able to gain access to the required information directly or through unique paths (drive letters and directory paths).

Using cmdlets you can work with objects file system and certificate stores, installed applications and services.

Conveyor

When creating not very primitive scripts or to perform any operations with data obtained as a result of the script, sometimes you also have to perform some actions.

There is a conveyor for this. As in UNIX, it combines commands by passing the output of one cmdlet into the input of another, unchanged, preserving its type.

In this case, no container or character-by-character parsing of information is required.

The transmitted information may also include a function. After finishing the work of the combined commands, the function of converting information into text view(converting data to strings) using text formatting.

Scripts

Despite the fact that the shell allows you to automate many actions, you need to enter commands manually, which is not very convenient.

Especially when the same operations must be performed over and over again.

And you can’t do without the human factor: typos, errors, accidentally hitting keys when writing lines of code, correcting or retyping a line requires a more user-friendly attitude.

To perform monotonous chains of actions in PS, scripts are implemented - text files with a sequence of commands inside that are understandable to the interpreter.

Scripts in PowerShell will further simplify and automate the work on the PC and its maintenance, especially if the script contains branches, conditions, logical operations and cycles.

But here “all that glitters is not gold”: writing your own script or uploading a ready-made one (albeit slightly tailoring it to your own goals) will not work out that easily.

If inMicrosoftit was allowed no matter how much trouble made scripts forPowerShell , written by scammers and ill-wishers for selfish or hooligan purposes.

To run on Windows PS, you must pass its launch control.

If it fails, and the script must be executed, you will have to change the configuration of the objects responsible for the security of macros.

Before using scripts

When using VBS, many problems arise, which the PowerShell developers got rid of completely if script security is not lowered to satisfy user script requests or solve problems related to the level of security of the operating system.

Running a downloaded script on a user's machine is a simple and effective option for spreading malware. software or theft of personal information by attackers.

Everything happens due to basic ignorance of the contents of the script file (ps1) and the desire to quickly solve their problems at the expense of the work of others. Objects that control the security level of script execution create an environment in which it is impossible to run the script.

Intentional running of macros occurs after changing the security level, if the user gives the go-ahead, realizing what he is doing, and knowing for sure what is in the *.ps1 file.

Because security configuration is inconvenient for people who need to implement and run their own scripts, it is possible to change the configuration.

Here you can either reduce protection to a minimum or wisely maneuver between the ability to open holes for penetration and ease of working with PowerShell.

The shell has three levels of security:

  • files with the ps1 extension are not identified by the system as executable and are designated as unknown or text (by double clicking they open in the default text editor used on the computer);
  • The shell allows you to execute scripts after specifying their full path, script files in the current directory are not searched, making it impossible to execute macros located in the current directory;
  • running the built-in Execution Policy script, responsible for adding the required one to the list of allowed scripts.

Don't even think about changing the configuration, lowering it to at least the second level, without familiarizing yourself with the basicsPowerShelluntil you begin to at least superficially understand the contents of *.ps1.

There is also the concept of an execution policy, created to prevent scripts from being accidentally executed. There are five launch policy settings:

  • limited – installed by default, only scripts signed by Microsoft are executed, allowing you to obtain information about hardware and shell computer;
  • having a signature deleted files – you can execute all macros, but files sent by mail must be signed;
  • having digital signature from a reliable source– all signed scripts are executed;
  • unlimited– launch any macros;
  • bypass– are intended for programmers who create their own security system, rather than using the one provided in the shell.

You can check the current state of the execution policy using the “Get-ExecutionPolicy” command.Whatever the security policy, the user will not be able to run a script that contains commands that he does not have enough privileges to execute.

Launching PowerShell

It's time to move from theory to practice. So, to execute scripts you need to obtain a digital signature or (the second option is simpler).

There are several ways to open a PowerShell window.

It is presented in two forms:

  • classic console;

  • PowerShell ISE - adds support for tabs, syntax, contextual help, contextual and main menus, which greatly facilitates working in the interpreter.

Start

The easiest way to call PS is through Start.

  1. Open the menu(in Windows 7, click “All Programs”).
  2. Go to the Windows PowerShell directory and click on the desired icon.

Rice. 4 – Launch PS via Start

You can also call PS via integrated search system.

Command interpreter

Who loves non-standard and quick ways launch, use the “Run” window. It opens with the button of the same name in Start and the key combination Win + R.

In the dialog box we write "powershell" and press “Enter”.

Win+X

In Windows 10, PS can be called up from the WinX menu. The fact is that by default by clicking on the command "Command line" open CMD. It can be replaced with PowerShell.

Open the “Properties” of the Taskbar, in the “Navigation” tab, put a checkmark next to the only option and save the settings.

You can call PowerShell through the command line by writing “powershell” in it or by running the executable file along the path: %WINDIR%\ System32\ WindowsPowerShell\v1.0 for 32-bit systems and at %WINDIR%\ syswow64\ WindowsPowerShell\v1.0 for 64-bit Windows any editors.

Rice. 8 – Run PS from the directory where its executable file is stored

Setting up a security policy

Left a little– allow PowerShell to execute scripts. Enter the command “Set-ExecutionPolicy RemoteSigned” and press “Y”.

That's all. Now you can run any cmdlets and scripts. Let's try, for example, to display a list of active processes by executing "Get-Process".

Running scripts

You can create scripts in any (it’s better to stick with Win, or something similar, with syntax or validation support) or in the PowerShell ISE program.

The latter provides convenient access to cmdlets, the ability to run, debug and save scripts.

Let's assume that you wrote your first script (this is to obtain a list of running processes), saved it through the "File" menu, and you need to run it. This is done in three ways:

1 We write the full path to the script in PowerShell (or ISE);

Rice. 13 – Running scripts from the PowerShell ISE window

Important so that there are no spaces in the path to the file!

PowerShell– a powerful tool for simplifying routine operations in Server 2008 and newer. An important advantage of PowerShell is scripts and availability version of the program from graphical interface PS ISE. The commands in it are presented in the form of cmdlets, which will make it possible to familiarize yourself with the shell and understand the meaning of each command. The security policy will not allow a beginner to harm his computer scripts received from unknown sources, and the debugging mode, creating your own cmdlets, functions and error handling mechanisms open up unlimited possibilities for programmers and administrators.