Powershell Study Notes

Source: Internet
Author: User

1. Get the number of a set

@ (Get-process | where-object {$ _. Name-EQ "service1"}). Count

Note:

If "@" is not added, null (rather than 0) is returned when count is 0 ).

 

2. Create a custom Method

Function finddefaprinprinter

{

Get-wmiobject-query "select * From win32_printer Where default = true"

}

 

3. Display calculated fields
C: \ PS> Get-process | select-object-property processname, @ {name = "start day"; expression ={$ _. starttime. dayofweek }}

Processname startday

------------

ALG Wednesday

Ati2evxx Wednesday

Ati2evxx Thursday

...

Description

-----------

This command displays the name and start date of the running process on the computer.

The value of the property parameter is the computing property of processname and "start day. Add the "Start Day" attribute by using a hash table with the name and expression keys.

 

4. Get Command alias
Get-alias

 

5. Create a profile file
New-item-path $ profile-type file-Force

Edit the profile file:

Notepad $ Profile

 

6. Get/set the execution Policy
Get-executionpolicy

Set-executionpolicy remotesigned

Obtain signature help information

Get-help about_signing

 

7. Change the current path
PS c: \ scripts> set-Location C: \ WINDOWS

Ps c: \ Windows>

 

8. Execute the powershell script in VBScript
Set objshell = Createobject ("wscript. Shell ")

Objshell. Run ("powershell.exe-noexit c: \ scripts \ test. PS1 ")

 

9. Run the powershell script in windows.

Powershell.exe-noexit get-childitem c: \ Users \ Administrator \ Desktop

Powershell.exe "C: \ Users \ Administrator \ Desktop \ test. PS1"

Powershell.exe-noexit "C: \ Users \ Administrator \ Desktop \ test. PS1"

Note:

"&" Cannot be added before the file path string after 110000powershell.exe; otherwise, the script file will be opened in Notepad rather than executed by powershell.

2) the file path string cannot be enclosed in single quotation marks. Otherwise, the string will be "Echo" rather than executed.

 

10. Read and add Environment Variables

$ A = $ ENV: path; $ A. Split (";")

$ ENV: Path = $ ENV: path + "; C: \ scripts"

 

11. Pipeline example

Get-pssnapin | select name | where-object {$ _. Name-contains "Microsoft. Sharepoint. powershell "}

Get-childitem C: \ WINDOWS | where-object {$ _. Length-GT 200kb} | sort-object Length

Get-service | sort-object status | format-table

 

12. How to display strings

Foreach ($ service in get-content c: \ services.txt ){

Set-service $ service-startuptype disabled

"Disabling $ service"

}

 

13. How to Use the pipeline operator (|)

PS c: \> Get-content c: \ services.txt |

Set-service-startuptype disabled

PS c: \>

The following is what will happen:

1) run the get-content cmdlet to read the entire file. Each row in the file is processed as a unique object and put together as a collection of objects.

2) deliver the object set to set-service through a pipeline.

3) For each input object, the pipeline executes the set-service cmdlet once. For each execution, you must pass the input object as the first parameter of the cmdlet (that is, the service name) to set-service.

4) Set-Service uses the input object of its first parameter and any other specified parameters (in this example, the-startuptype parameter) for execution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.