Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Windows PowerShell Learning-Chapter 1 introduction to PowerShell

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Chapter 1 PowerShell introduction 1.1.What is PowerShell

In 2006, Microsoft released a new scripting language called Windows PowerShell.

PowerShell, like the command prompt shell, PowerShell can enter commands interactively. You can also easily concatenate files and programs using pipes and redirects.

However, PowerShell is a powerful object-oriented language that can be used for complex scripting.

1.2. object-oriented command shell

About cmd

If you enter the dir,dir command at the command prompt, the file name, size, and timestamp in the directory will be printed on the screen. You can use the > redirect operator to import the text into a file, or you can use the | operator to connect the text pipe to another program. For example:

Dir / s / b | sort

The above command lists the files in the current directory and its subdirectories as a stream of text, and the text is sent to the sort program, and sort sorts them alphabetically.

PowerShell and object

The PowerShell command seems to work in the same way, but its command line deals with objects rather than text. This object is like the object-oriented object in Java. These objects represent files, folders, Windows device drivers, objects for network services, and objects that record any of the hundreds of objects defined in the .NET Framework library.

Flow from one command to the next through a pipe symbol, which is an object stream. Various PowerShell commands can generate and manipulate these objects, call methods on these objects, modify their properties, and extract information from these objects.

About cmdlet

Cmdlet is pronounced command-let.

The PowerShell built-in command is called cmdlet. The reason for such a strange name may be that they are not as completely built into PowerShell as cmd.exe 's commands, nor are they really as independent of PowerShell as .exe files. They are implemented in a new way, so a new word is needed.

Some simple instructions for Cmdlet

Cmdlet names are not case sensitive.

Microsoft programmers chose to use a noun-verb convention to name cmdlet.

This gives the name a clearer picture of what cmdlet actually does.

The name of the Cmdlet command line option is equally long.

Some commands can be shortened to shorter.

For example, you can use the new-alias command to give cmdlet a short alias.

What happens after the dir command is entered in PowerShell

The PowerShell dir command is actually an alias for Get-ChildItemcmdlet. You can enter either of these two names to get the same result.

Without other parameters, Get-ChildItem lists File and Folder objects for all files and subdirectories in the current directory. If you enter the command dir without arguments, Get-ChildItem displays a list of File and Folder objects, and the result appears in the Windows PowerShell command window because there is no pipe or output redirection.

When an object enters the PowerShell window, PowerShell prints a row for each object, listing each object's most important properties in a beautiful table format. For example:

PSE:\ py > dir

Directory: e:\ py

Mode LastWriteTime Length Name

-a Murray-2015-11-18 18:28 30 a.txt

-a murmuri-2015-8-6 18:09 1510 t1.py

-a murmuri-2015-9-13 13:15 320 t2.py

-a murmuri-2015-9-13 12:57 37 t2.txt

-a Murray-2015-11-19 14:40 224 t3.py

PSE:\ py >

The title at the top of each column is the name of the attribute to be displayed. This list is similar to any other object type generated by cmdlet.

The dir command in the regular command prompt environment produces text in a fixed format. Dir cmdlet in PowerShell, which generates a list of File and Folder objects, which PowerShell then formats into a text list.

You can use the > symbol to redirect the output of an PowerShell cmdlet to a file; and the same thing happens: the object stream is formatted into a beautiful text list.

The innovation of PowerShell is what happens when pipes are used. PowerShell allows you to direct a stream of objects from one cmdlet to another, and you can modify the properties of those objects and call methods on them as if the objects were actually passed. The text appears only when the object finally touches the screen. A cmdlet can produce a list of objects that represent files, computers, services, and network objects; a next cmdlet in a pipe can filter and pass only those objects it is interested in; the next cmdlet may call a method to perform an operation on the object. This is really a unique feature of PowerShell.

For example, delete files in a specific directory

PSD:\ ps > dir

Directory: d:\ ps

Mode LastWriteTime Length Name

-a Murray-2017-9-9 17:00 01-copy (2). Txt

-a Murray-2017-9-9 17:00 01-copy (3). Txt

-a Murray-2017-9-9 17:00 01-copy (4). Txt

-a Murmuri-2017-9-9 17:00 01-copy. Txt

-a murmuri-2017-9-9 17:00 0 1.txt

PSD:\ ps > dir | Remove-Item

PSD:\ ps > dir

PSD:\ ps >

Dir generates a list of File and Folder objects that represent the contents of the directory d:\ ps and passes it to the remove-item command, and remove-item removes the "real content" behind any objects passed to it.

Or you can use the following command.

(dir d:\ ps) .delete ()

It generates the same stream of file and folder objects and calls the delete method on each object. The result is the same: the files have been deleted.

You can also go like this:

Remove-itemd:\ ps\ *. *

This will be the most straightforward command, but it doesn't show the difference between PowerShell and a regular command prompt.

Here is another example of how a pipe command works.

PSD:\ ps > dir

Directory: d:\ ps

Mode LastWriteTime Length Name

-a murmuri-2017-9-9 17:12 0 1.txt

-a murmuri-2017-9-9 17:12 0 2.txt

PSD:\ ps > dir | Where-Object {$_ .name-eq "1.txt"}

Directory: d:\ ps

Mode LastWriteTime Length Name

-a murmuri-2017-9-9 17:12 0 1.txt

PSD:\ ps > dir | Where-Object {$_ .name-eq "1.txt"} | remove-it

PSD:\ ps > dir

Directory: d:\ ps

Mode LastWriteTime Length Name

-a murmuri-2017-9-9 17:12 0 2.txt

PSD:\ ps >

Dir generates an object for each file in the current directory, where-Object is used to filter, filter out the object with the file name 1.txt, and delete the file with remove-item.

1.3.Based on .NET Framework

.net Framework is Microsoft's response to sun's Java programming language. It contains a powerful class library, which can use graphical user interface, database access, network communication, web interaction, encryption, numerical calculation and other functions.

All the features of the .NET Framework are available on the PowerShell command line and in scripts.

.net Framework class library online address

Https://msdn.microsoft.com/zh-cn/library/w0x726c2(v=vs.110).aspx

1.4. A scalable environment

PowerShell can run three kinds of programs: built-in commands, external programs, and scripts.

This is similar to a regular command prompt environment, where you can use built-in commands handled by the cmd program itself, run external programs, or create batch files that combine various types of commands to perform a more complex task.

In PowerShell, the built-in command is cmdlet. However, unlike the command prompt shell, these built-in commands are not solidified into the PowerShell program, but are added to the PowerShell program through a plug-in method as one or more .DLL files stored on the hard disk.

Therefore, custom cmdlet can be added to the environment. The idea is that Microsoft and third parties can add installation management cmdlet to their applications and servers so that they can be managed by PowerShell scripts. For example, Microsoft SQL Sever, Exchange, and VMWare servers have custom cmdlet plug-ins,

1.5. PowerShell environment

Start à all procedures à Annex à Windows PowerShell

Windows PowerShell interactive command environment

Windows PowerShell ISE this is a GUI editing / debugging tool that can be used to develop PowerShell scripts.

1.6. PowerShell version

Win 7 and Windows server 2008R2 install v2.0 by default

1.7. Get help

The first thing to learn about the most important PowerShell command is to help you understand one of the other commands: get-help.

Get-help has an alias help. So any of these command names can be used interchangeably. PowerShell has a lot of built-in help.

Some techniques are as follows:

Enter the word help to get a quick introduction to the online help system

PSE:\ py > help

Theme

Get-Help

A short description

Displays help on Windows PowerShell cmdlet and concepts.

detailed description

Grammar

Get-help {|}

Help {|}

-?

"Get-help" and "?" Displays help as a single page.

Help displays help in a multi-page format.

Example:

Get-help get-process: displays help on Get-Process cmdlet.

Get-help about_signing: displays help on signing scripts.

Help where-object: displays help on Where-Object cmdlet.

Help about_foreach: displays help on foreach loops in PowerShell.

Set-service -?: displays help on Set-Service cmdlet.

You can use wildcards in help commands (without using -?).

If there is more than one matching help topic, PowerShell displays a list of matching topics.

If there is only one matching help topic, PowerShell displays that topic.

Example:

Get-help *: displays all help topics.

Get-help get-*: displays topics that start with get-.

Help * object*: a topic with "object" in the display name.

Get-help about*: displays all conceptual topics.

For information about wildcards, type:

Get-help about_wildcard

Remarks

To learn about Windows PowerShell, read the following help topics:

Get-command: get information about cmdlet from the cmdlet code.

Get-member: gets the properties and methods of the object.

Where-object: filter object properties.

About_object: introduces the use of objects in Windows PowerShell.

About_remote: explains how to run commands on a remote computer.

The conceptual help file is named in the form "about_", for example: about_regular_expression.

The name of the conceptual help file must be entered in English, even in a non-English version of Windows PowerShell.

The help command can also display an alias for cmdlet. These aliases are aliases or abbreviations that are easier to type.

For example, the alias for Invoke-Command cmdlet is "remote".

To get an alias, type:

Get-alias

PSE:\ py >

After help or get-help, you can enter a word, a cmdlet name, or a partial cmdlet name.

You can also enter a phrase enclosed in quotation marks.

If there are multiple topics, multiple cmdlet names, or multiple requests that match the input, get-help prints a list of all matching help entries. You can then type help, followed by the name of the specific item you want to know in detail.

PSC:\ Users\ Administrator > Get-Help new

Name Category Synopsis

-

New-WSManInstance Cmdlet creates a new instance of managing resources.

New-WSManSessionOption Cmdlet creates a hash table of WS-Management session options to use as input parameters for the following WS-Management cmdlet:...

New-PSSession Cmdlet establishes a persistent connection to a local or remote computer.

New-PSSessionOption Cmdlet creates an object that contains advanced options for PSSession.

New-Module Cmdlet creates a new dynamic module that exists only in memory.

New-ModuleManifest Cmdlet creates a new module list.

New-Event Cmdlet creates a new event.

New-Alias Cmdlet creates a new alias.

New-TimeSpan Cmdlet creates a TimeSpan object.

New-Object Cmdlet creates an instance of a Microsoft .NET Framework or COM object.

New-Variable Cmdlet creates a new variable

New-EventLog Cmdlet creates a new event log and a new event source on the local or remote computer.

New-PSDrive Cmdlet creates a Windows PowerShell drive in the current session.

New-Item Cmdlet creates a new item.

New-ItemProperty Cmdlet creates a new property for the item and sets the value of the property. For example, you can use New-ItemProperty to create and change registry values and data, which are properties of registry keys.

New-Service Cmdlet creates a new Windows service.

New-WebServiceProxy Cmdlet creates a Web service proxy object for using and managing Web services in Windows PowerShell.

If there is indeed a name or description that contains the word or phrase entered, get-help can print out help for that topic.

PSC:\ Users\ Administrator > Get-Help new-ite

Name Category Synopsis

-

New-Item Cmdlet creates a new item.

New-ItemProperty Cmdlet creates a new property for the item and sets the value of the property. For example, you can use New-ItemProperty to create and change registry values and data, which are properties of registry keys.

_ _

PSC:\ Users\ Administrator > Get-Help new-item

Name

New-Item

Abstract

Create a new item.

Grammar

New-Item [- Path] [- Credential] [- Force] [- ItemType] [- Value] [- Confirm] [- WhatIf] [- UseTransaction] []

New-Item-Name [[- Path]] [- Credential] [- Force] [- ItemType] [- Value] [- Confirm] [- WhatIf] [- UseTransaction] []

Description

New-Item cmdlet creates a new item and sets the value of the item. The type of item that can be created depends on where it is located. For example, in the file system, New-Item is used to create files and folders. In the registry, New-Item is used to create registry keys and registry entries.

In addition, New-Item can set the value of the item it creates. For example, when you create a new file, New-Item can add initial content to the file.

Related links

Online version: http://go.microsoft.com/fwlink/?LinkID=113353

About_Providers

Get-Item

Set-Item

Remove-Item

Clear-Item

Invoke-Item

Rename-Item

Move-Item

Copy-Item

Remarks

To see an example, type: "get-help New-Item-examples".

For more information, type: get-help New-Item-detailed.

To get technical information, type: "get-help New-Item-full".

Some cmdlet has additional help information to use.

For example, you can see some examples of using new-alias cmdlet by typing help new-alias-examples.

Common options for additional information are-examples,-detailed,-full.

The help text appears in the console window and is piped through more by default so that only it is paused on each screen. Press enter to continue to the next page.

In addition, it is relatively easy to output the help information to a file, > x; then type notepad x to read the text.

There are help entries that cover each of the installed cmdlets, as well as additional articles on different topics.

These articles begin with about_, for example, help about_execution_policies prints out information about the script security restrictions system.

To view a list of all these about entries, enter help about.

The online help system works well to find cmdlet according to the words in the description of the work done by cmdlet, and to know the command line syntax of a cmdlet you are interested in.

Online help syntax description

[] Square brackets indicate optional command parameters

{} curly braces usually indicate that you can select a series of options with a vertical line between them.

The values enclosed in angle brackets must be provided by yourself.

For example, [- Description] represents an optional parameter. You can ignore it, or type something like-description "some text". You can also omit it to-descr "sometext".

1.8, alias

(1) get a list of aliases

See a list of all built-in aliases by typing alias. Alias is also an alias for get-alias

PSC:\ Users\ Administrator > alias

CommandType Name Definition

-

Alias ForEach-Object

Alias? Where-Object

Alias ac Add-Content

Alias asnp Add-PSSnapIn

Alias cat Get-Content

Alias cd Set-Location

Alias chdir Set-Location

Alias clc Clear-Content

Alias clear Clear-Host

Alias clhy Clear-History

Alias cli Clear-Item

Alias clp Clear-ItemProperty

Alias cls Clear-Host

Alias clv Clear-Variable

Alias compare Compare-Object

Alias copy Copy-Item

Alias cp Copy-Item

Alias cpi Copy-Item

Alias cpp Copy-ItemProperty

Alias cvpa Convert-Path

Alias dbp Disable-PSBreakpoint

Alias del Remove-Item

Alias diff Compare-Object

Alias dir Get-ChildItem

Alias ebp Enable-PSBreakpoint

Alias echo Write-Output

Alias epal Export-Alias

Alias epcsv Export-Csv

Alias epsn Export-PSSession

Alias erase Remove-Item

Alias etsn Enter-PSSession

Alias exsn Exit-PSSession

Alias fc Format-Custom

Alias fl Format-List

Alias foreach ForEach-Object

Alias ft Format-Table

Alias fw Format-Wide

Alias gal Get-Alias

Alias gbp Get-PSBreakpoint

Alias gc Get-Content

Alias gci Get-ChildItem

Alias gcm Get-Command

Alias gcs Get-PSCallStack

Alias gdr Get-PSDrive

Alias ghy Get-History

Alias gi Get-Item

Alias gjb Get-Job

Alias gl Get-Location

Alias gm Get-Member

Alias gmo Get-Module

Alias gp Get-ItemProperty

Alias gps Get-Process

Alias group Group-Object

Alias gsn Get-PSSession

Alias gsnp Get-PSSnapIn

Alias gsv Get-Service

Alias gu Get-Unique

Alias gv Get-Variable

Alias gwmi Get-WmiObject

Alias h Get-History

Alias history Get-History

Alias icm Invoke-Command

Alias iex Invoke-Expression

Alias ihy Invoke-History

Alias ii Invoke-Item

Alias ipal Import-Alias

Alias ipcsv Import-Csv

Alias ipmo Import-Module

Alias ipsn Import-PSSession

Alias ise PowerShell_ise.exe

Alias iwmi Invoke-WMIMethod

Alias kill Stop-Process

Alias lp Out-Printer

Alias ls Get-ChildItem

Alias man help

Alias md mkdir

Alias measure Measure-Object

Alias mi Move-Item

Alias mount New-PSDrive

Alias move Move-Item

Alias mp Move-ItemProperty

Alias mv Move-Item

Alias nal New-Alias

Alias ndr New-PSDrive

Alias ni New-Item

Alias nmo New-Module

Alias nsn New-PSSession

Alias nv New-Variable

Alias ogv Out-GridView

Alias oh Out-Host

Alias popd Pop-Location

Alias ps Get-Process

Alias pushd Push-Location

Alias pwd Get-Location

Alias r Invoke-History

Alias rbp Remove-PSBreakpoint

Alias rcjb Receive-Job

Alias rd Remove-Item

Alias rdr Remove-PSDrive

Alias ren Rename-Item

Alias ri Remove-Item

Alias rjb Remove-Job

Alias rm Remove-Item

Alias rmdir Remove-Item

Alias rmo Remove-Module

Alias rni Rename-Item

Alias rnp Rename-ItemProperty

Alias rp Remove-ItemProperty

Alias rsn Remove-PSSession

Alias rsnp Remove-PSSnapin

Alias rv Remove-Variable

Alias rvpa Resolve-Path

Alias rwmi Remove-WMIObject

Alias sajb Start-Job

Alias sal Set-Alias

Alias saps Start-Process

Alias sasv Start-Service

Alias sbp Set-PSBreakpoint

Alias sc Set-Content

Alias select Select-Object

Alias set Set-Variable

Alias si Set-Item

Alias sl Set-Location

Alias sleep Start-Sleep

Alias sort Sort-Object

Alias sp Set-ItemProperty

Alias spjb Stop-Job

Alias spps Stop-Process

Alias spsv Stop-Service

Alias start Start-Process

Alias sv Set-Variable

Alias swmi Set-WMIInstance

Alias tee Tee-Object

Alias type Get-Content

Alias where Where-Object

Alias wjb Wait-Job

Alias write Write-Output

(2), define aliases

You can enter the following command to define a new alias:

New-alias-nameshortname-value realcommandname-description "Brief description"

The alias definition no longer exists when the PowerShell window is closed. The next time you run PowerShell, the custom aliases will no longer exist.

If you want to save the alias, you can do so in the form of a configuration file.

1.9. PowerShell configuration file

You can customize your preferred PowerShell environment by adding custom aliases and directories to the path-environment variable. So every time you start PowerShell, it's painful to have to re-enter these commands. Script

The configuration file for PowerShell is a script for the commands that PowerShell runs each time a new instance is started.

In fact, whenever and in what form PowerShell is launched, it looks for configuration file scripts in the following two locations:

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

C:\ Users\ username\ Documents\ WindowsPowerShell\

Look for a script called profile.ps1 and run it if you find it.

The command line PowerShell then looks for a configuration file script called Microsoft.PowerShell_profile.ps1 and executes it if it is found.

The GUI PowerShellISE program looks for profile.ps1 first, then Microsoft.PowerShellISE_profile.ps1.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report