
powershell 入門

PowerShell is quickly becoming the preferred scripting language and CLI of Power Users as well as IT Pros. It’s well worth learning a few commands to get you started, so we’ve got 5 useful cmdlets for you to learn today.
PowerShellSwift成為高級用戶和IT專業人員的首選腳本語言和CLI。 值得學習一些命令以使您入門非常值得,因此,我們今天有5個有用的cmdlet供您學習。
獲取命令 (Get-Command)
The Get-Command is one of the most useful cmdlets in the whole of PowerShell, as it will help you getting to grips with PowerShell by letting you search for certain cmdlets. Using Get-Command on its own is admittedly not very useful as its just going to spit out every command that PowerShell has.
Get-Command是整個PowerShell中最有用的cmdlet之一,因為它可以通過搜索某些cmdlet來幫助您掌握PowerShell。 單獨使用Get-Command并不是很有用,因為它只會吐出PowerShell所具有的每個命令。

But from this we can see that that objects that PowerShell outputs have both a Name and a ModuleName property. Using this information we can fine grain our search, by searching for cmdlets that contain certain terms. For example if I wanted to find all cmdlets that contain the word “IP”, I could do this:
但是從中我們可以看到PowerShell輸出的對象同時具有Name和ModuleName屬性。 使用此信息,我們可以通過搜索包含某些術語的cmdlet來細化搜索范圍。 例如,如果我想查找所有包含單詞“ IP”的cmdlet,則可以這樣做:
Get-Command –Name *IP*
Get-Command –名稱* IP *

As you can see we still get quite a few results, our next option is to search within a specific module. In our case i will choose the NetTCPIP module.
如您所見,我們仍然可以獲得很多結果,我們的下一個選擇是在特定模塊內搜索。 在我們的情況下,我將選擇NetTCPIP模塊。
Get-Command –Module NetTCPIP –Name *IP*
獲取命令–模塊NetTCPIP –名稱* IP *

得到幫助 (Get-Help)
Once you have found the cmdlet you are looking for using Get-Command, you are going to want to know the syntax and how you can use that specific cmdlet. This is where Get-Help comes in, if you have ever used the command line in Windows you probably did something like this:
找到要使用Get-Command查找的cmdlet后,您將想要了解語法以及如何使用該特定cmdlet。 這是獲取幫助的地方,如果您曾經在Windows中使用過命令行,則可能會執行以下操作:
ipconfig /?
ipconfig /?
Well that doesn’t work in PowerShell, this is because in PowerShell a space is used to separate a command from its parameters. So instead we use Get-Help and pass a cmdlets name to Get-Help as a parameter.
嗯,這在PowerShell中不起作用,這是因為在PowerShell中,空格用于將命令與其參數分開。 因此,我們改為使用Get-Help并將cmdlet名稱作為參數傳遞給Get-Help。
Get-Help Get-Process
獲取幫助獲取過程

獲得會員(Get-Member)
Get-Member allows us to get information about the objects that a cmdlets returns. The catch with get-member, is that it relies on PowerShell’s pipeline feature, to demonstrate this, we will can use the Get-Process cmdlet.
Get-Member允許我們獲取有關cmdlet返回的對象的信息。 get-member的不足之處在于,它依賴于PowerShell的管道功能,對此進行演示,我們將可以使用Get-Process cmdlet。

As you can see PowerShell’s output shows us some of the properties, which you can see at the top of each column. The first problem is that, while those are the properties you might be looking for most of the time, there are still more of them. The second problem is that it doesn’t show any methods that we are able to call on the object. To see the methods and properties we can pipe our output to Get-Member, like so:
如您所見,PowerShell的輸出向我們顯示了一些屬性,您可以在每一列的頂部看到這些屬性。 第一個問題是,盡管大多數時候這些屬性是您可能一直在尋找的,但仍有更多的屬性。 第二個問題是它沒有顯示我們可以在對象上調用的任何方法。 要查看方法和屬性,我們可以將輸出通過管道傳遞到Get-Member,如下所示:
Get-Process | Get-Member
獲取流程| 獲得會員

While it may mean nothing to you right now, you will sooner or later need to use Get-Member, and the sooner you learn to use it the better. As an example, using the information from the output we could do something like:
盡管這可能對您現在沒有任何意義,但是您遲早需要使用Get-Member,并且越早學會使用它就越好。 例如,使用輸出中的信息,我們可以執行以下操作:
Start-Process notepad.exe $NotepadProc = Get-Process -Name notepad $NotepadProc.WaitForExit() Start-Process calc.exe
Start-Process notepad.exe $ NotepadProc =獲取進程-Name記事本$ NotepadProc.WaitForExit()Start-process calc.exe
That script will launch notepad,? it then assigns output of “Get-Process –Name notepad” to the $NotepadProc variable, then we call the WaitForExit method on $NotepadProc which causes the script to pause? until you close notepad, once you have closed notepad then the calculator will launch.
該腳本將啟動記事本,然后將“ Get-Process –Name notepad”的輸出分配給$ NotepadProc變量,然后在$ NotepadProc上調用WaitForExit方法,這將導致腳本暫停,直到您關閉記事本為止。然后計算器將啟動。
$ _(當前管道對象) ($_(Current Pipeline Object))
While not exactly a cmdlet, it is one of the most used special variables in PowerShell. The official name for $_ is “the current pipeline object” . It is used in script blocks, filters, the process clause of functions, where-object, foreach-object and switches. However it is easier to explain with an example, which brings us to our next and final cmdlet, Where-Object.
雖然不完全是cmdlet,但它是PowerShell中最常用的特殊變量之一。 $ _的正式名稱是“當前管道對象”。 它用于腳本塊,過濾器,函數的處理子句,where對象,foreach對象和開關。 但是,通過示例更容易解釋,該示例將我們帶入下一個也是最后一個cmdlet Where-Object。
對象在哪里 (Where-Object)
Where-Object does exactly what it sounds like, it selects an object based on whether it meets a certain criteria. This will bring together $_, and the properties we can see using Get-Member. To demonstrate this, we will pipe the output of Get-Process into the Where-Object cmdlet.
Where-Object完全按照聽起來的樣子運行,它根據是否滿足特定條件來選擇對象。 這將集合$ _和我們可以使用Get-Member看到的屬性。 為了說明這一點,我們將把Get-Process的輸出傳遞到Where-Object cmdlet中。
Get-Process | Where-Object {$_.Name –eq “iexplore”}
獲取流程| 哪里對象{$ _。Name –eq“ iexplore”}

So what’s going on here you ask? Well the first thing we are doing is getting a list of processes on our computer and passing the output (using the | character) to our Where-Object cmdlet,? which takes a script block? as a parameter. The script block (defined by the curly braces) instructs the Where-Object cmdlets to only select objects where their name parameter is equal to “iexplore”, and so we only get a list of the IE instances that are running. That’s all there is to it, have fun!
那你問這是怎么回事? 好吧,我們要做的第一件事是獲取計算機上的進程列表,并將輸出(使用|字符)傳遞給Where-Object cmdlet,該cmdlet將腳本塊作為參數。 腳本塊(由花括號定義)指示Where-Object cmdlet僅選擇其名稱參數等于“ iexplore”的對象,因此,我們僅獲得正在運行的IE實例的列表。 這就是全部,玩得開心!
翻譯自: https://www.howtogeek.com/114344/5-cmdlets-to-get-you-started-with-powershell/
powershell 入門