Powershell _ basic self-study course _ 4_powershell alias function, error management function, and system resource area navigation

Source: Internet
Author: User
Tags foxit software

In a previous article, we mentioned that there can be a very convenient alias mechanism in PS, which is helpful for people familiar with other console shells to use ps. However, some of these problems are only

So far, let's talk about this topic in detail today.

1. Use aliases

In PS, you can create aliases for functions, cmdlets, or executable files, and then use aliases instead of original names in any command. (Here is an irrelevant topic. Some children's shoes may

In Windows, DLL is considered not an executable file. In PS, you can run the get-command * command to view the type of executable files in windows)

PS also has many built-in aliases, which can be viewed through the get-alias cmdlet.

An important concept should be introduced: the alias drive has a built-in environment support object in PS, that is, the alias drive; its drive letter is alias:; note that this drive letter is a virtual

Instead of the actual partition drive letter. In PS, use CD alias: to enter this virtual drive letter. :

Obviously, the current working directory is switched to the alias virtual drive letter. You can use the get-childitem command to get the content under the Virtual Drive ,:

The alias virtual drive letter actually stores the command alias.

2. Create an alias

It is easy to create an alias by using the set-alias cmdlet. The syntax rules are as follows:

Set-alias cmdlet

Exp:

Set-alias ls get-childitem

If you create an alias for the application and executable file in the path system environment variable, you do not need to specify the full path. If the application name contains spaces, the rules mentioned in the first article must be used,

When creating an alias for an executable file, it must contain the complete path or add the path to the path system environment variable.

Exp:

Set-alias NP notepad # Because notepad is in the path of the PATH environment variable, You can execute the command like this

The preceding statement is equivalent to the following statement:

Set-alias NP c: \ windows \ notepad.exe (the path may be different on different systems)

3. delete an alias

Now that you can set aliases, you must be able to delete them. In PS, you can use the remove-item cmdlet to delete aliases from the alias drive, just like deleting a file in the driver.

Exp:

Remove-item alias: \ NP

In this way, the NP alias can be deleted from the alias drive.

4. Use a function to replace a command with Parameters

The set-alias command cannot be used to create aliases for commands with parameters.

Exp:

Set-alias NP-profile "Notepad $ profile" # this alias is incorrect.

The functions provided by PS can be used to implement aliases of commands with parameters.

Exp:

Function NP-profile {notepad $ profile}

The defined function has a similar function as the alias. If you type NP-profile in PS, the profile is opened for editing.

5. Use Windows Applications

In PS, you can start Windows applications. If the application outputs text, you can capture the output text for analysis and use. You can also

To start the application on the GUI, just like starting notepad.

In PS, there is a limit on starting Win32 EXE files. Only the EXE files under the system environment variable $ path can be started. The $ PATH environment variable indicates the paths that can be searched by PS. Use ENV Environment in PS

Variable object to obtain the path. As shown in:

To add a path to the default search path, you only need to do the following:

Exp:

$ ENV: path + = "; H :\"

In this way, add the h root directory to the system's default search path. Note: The string only loads the + = symbol, but does not load the-= symbol.

Note that the added path is only valid for the current session and is no longer valid after the current session is launched. You must modify the configuration file if it remains valid. The configuration file modification method is described in the previous article.

6. Management Error

Errors are often encountered during the use of PS. There are two types of errors in PS: Termination error and non-termination error.

Termination error: When a termination error occurs, the execution of the command will be terminated. For example, if the file cannot be deleted, the PS will continue to run regardless of the error, and an error and output will be displayed.

Non-termination error: the command is not terminated. For example, if an invalid operation object is submitted, PS generates a termination error.

7. Navigation in PS

Ps provides the most groundbreaking feature: Virtual Drive navigation. In PS, in addition to browsing in the file system drive, you can also browse in HKEY_LOCAL_MACHINE (HKLM :) and

HKEY_CURRENT_USER (hkcu :) registry Configuration unit drive. You can also browse in the digital signature certificate storage area (CERT :) and functions in the current session. These drivers

Is a Windows PS drive.

You can view the driver units supported by PS through: Get-psdrive. As shown in:

7. 1 navigation/browsing in the file system

Use: set-Location/CD (CD is the alias of the Set-location cmdlet) to switch the path of the file system.

You can use get-childitem/DIR/LS (Dir, LS is the alias of the get-childitem cmdlet) to view objects in the file system path.

In the file system: the drive must be followed by ":", such as cd d:, which is the same as that in cmd. The sub-directories and parent directories must be separated by slash \ or backslash;

PS inherits some of the CMD features, for example, "." indicates the current directory, "..." indicates the parent directory of the current directory, and "*" indicates the class capacity of the current directory.

Exp:

Get-item * can be used to obtain the content in the current directory. When a separate get-item is run, a prompt is displayed for the input path or operation object.

The built-in variable $ home in PS indicates the Home Directory of the current publisher, and $ pshome indicates the installation directory of Widnows powershell. Like other shells, you can perform directory and file operations in PS;

This is achieved through the relevant cmdlet below: Get-item, get-childitem, new-item, remove-item, set-item, move-item, and copy-item.

 

7. 2 browse the Registry

One of the most recommendable features of PS is the ability to browse and operate conveniently in the registry. The browsing method is consistent with the file browsing method. HKEY_LOCAL_MACHINE Configuration unit in PS

Ing to HKLM: Drive, while HKEY_CURRENT_USER is ing to hkcu: Drive. As shown in:

For example, you can use the following commands to view the key values of a subitem:

PS HKLM:\> cd software____________________________________________________________________________PS HKLM:\software> cd .\Microsoft____________________________________________________________________________PS HKLM:\software\Microsoft> cd .\PCHealth____________________________________________________________________________PS HKLM:\software\Microsoft\PCHealth> cd ..____________________________________________________________________________PS HKLM:\software\Microsoft> cd .\PowerShell____________________________________________________________________________PS HKLM:\software\Microsoft\PowerShell> cd 1____________________________________________________________________________PS HKLM:\software\Microsoft\PowerShell\1> cd .\ShellIds____________________________________________________________________________PS HKLM:\software\Microsoft\PowerShell\1\ShellIds> cd .\Microsoft.PowerShell____________________________________________________________________________PS HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell> dir____________________________________________________________________________PS HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell> ls____________________________________________________________________________PS HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell> get-itemproperty -path . PSPath          : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\softwar                  e\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShellPSParentPath    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\softwar                  e\Microsoft\PowerShell\1\ShellIdsPSChildName     : Microsoft.PowerShellPSDrive         : HKLMPSProvider      : Microsoft.PowerShell.Core\RegistryPath            : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exeExecutionPolicy : RemoteSigned

 

7. 3 browse the certificate Storage Area

As mentioned above, the digital signature certificate store maps to the CERT: Drive, so you can switch to this drive through: CD Cert: And you can view the content on the current drive through ls. :

You can also view other content in the current drive:

PS cert:\> dirLocation   : CurrentUserStoreNames : {SmartCardRoot, UserDS, AuthRoot, CA...}Location   : LocalMachineStoreNames : {SmartCardRoot, AuthRoot, CA, Trust...}_____________________________________________________________________________PS cert:\> cd currentuser_____________________________________________________________________________PS cert:\currentuser> dirName : SmartCardRootName : UserDSName : AuthRootName : CAName : TrustName : DisallowedName : MyName : RootName : TrustedPeopleName : TrustedPublisher

7. 4. Navigate to another drive

In addition to file systems, registries, and certificate drives, PS also has several other built-in drives, including the alias drive, Environment provider (ENV :), function (function :), and

Variable drive (variable :); you can browse these drivers by using the methods mentioned above.

Exp:

PS Env:\> lsName                           Value                                            ----                           -----                                            ALLUSERSPROFILE                C:\ProgramData                                   APPDATA                        C:\Users\vol_20120330\AppData\Roaming            CG_BOOST_ROOT                  C:\Program Files\Embarcadero\RAD Studio\7.0\in...CommonProgramFiles             C:\Program Files\Common Files                    COMPUTERNAME                   VOL_20120330_PC                                  ComSpec                        C:\Windows\system32\cmd.exe                      FP_NO_HOST_CHECK               NO                                               HOMEDRIVE                      C:                                               HOMEPATH                       \Users\vol_20120330                              LOCALAPPDATA                   C:\Users\vol_20120330\AppData\Local              LOGONSERVER                    \\VOL_20120330_PC                                MOZ_PLUGIN_PATH                C:\Program Files\Foxit Software\Foxit Reader\p...NUMBER_OF_PROCESSORS           4                                                OS                             Windows_NT                                       Path                           C:\Program Files\Embarcadero\RAD Studio\7.0\bi...PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.W...PROCESSOR_ARCHITECTURE         x86                                              PROCESSOR_IDENTIFIER           x86 Family 6 Model 37 Stepping 5, GenuineIntel   PROCESSOR_LEVEL                6                                                PROCESSOR_REVISION             2505                                             ProgramData                    C:\ProgramData                                   ProgramFiles                   C:\Program Files                                 PSModulePath                   C:\Users\vol_20120330\Documents\WindowsPowerSh...PUBLIC                         C:\Users\Public                                  SESSIONNAME                    Console                                          SystemDrive                    C:                                               SystemRoot                     C:\Windows                                       TEMP                           C:\Users\VOL_20~1\AppData\Local\Temp             TMP                            C:\Users\VOL_20~1\AppData\Local\Temp             USERDOMAIN                     vol_20120330_PC                                  USERNAME                       vol_20120330                                     USERPROFILE                    C:\Users\vol_20120330                            VS90COMNTOOLS                  C:\Program Files\Microsoft Visual Studio 9.0\C...windir                         C:\Windows                                       windows_tracing_flags          3                                                windows_tracing_logfile        C:\BVTBin\Tests\installpackage\csilogfile.log 

7, 5 ps drive

Windows PS supports the above extended drive navigation feature through the concept of PS drives; you can create PS drives in any valid Object Data Storage in windows; and you can create PS drives for these drives

Specify a valid drive letter, such as C: Or mydrive followed by a colon:; you can use the method mentioned above to navigate these PS drives. Note: The PS drive is only available in the PS environment.

Yes. Other Win32 programs cannot be viewed and accessed, for example, they cannot be viewed in assumer.exe or cmd.

You can use the new-psdrive cmdlet to create your own PS drive, as shown below:

PS env: \> New-psdrive-name mydocs-psprovider filesystem-root "$ home \ My roles ents" Warning: the column "currentlocation" cannot be displayed and has been deleted. Name used (GB) Free (GB) Provider root ---- ----------- -------- ---- mydocs 11.36 filesystem c: \ Users \ vol_20120330 \ my... _____________________________________________________________________________ PS env :\> CD mydocs: _____________________________________________________________________________ PS mydocs :\> lsget-childitem: Access to path "C: \ Users \ vol_20120330 \ My Documents" is denied. Location row: 1 character: 3 + ls <+ categoryinfo: permissiondenied: (C: \ Users \ vol_20120330 \ My docum ents \: string) [Get-childitem], unauthorizedaccessexception + fullyqualifiederrorid: dirunauthorizedaccesserror, Microsoft. powershell. c ommands. getchilditemcommand

8. Summary

PS functions are already quite powerful. If you know object-oriented ideas and understand C #, you will be able to understand PS functions. As the series of articles go deeper, the cross content of C # will be gradually introduced.

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.