Explore PowerShell (8) arrays and hash tables (Appendix: copy and paste skills)

Source: Internet
Author: User

We often use arrays in programming, which are also very common in scripts. This section describes in detail how to use arrays and hash tables in PowerShell.

Array

In PowerShell, when declaring a variable as an array, the symbol "@" is required. For example:

$strUsers=@(""user1","user2","user3) <enter>

In this way, we declare an array with three members. To view its value, use:

$strUsers <enter>

There are also some other operations, such as counting the number of members of an array:

$strUsers.Count <enter>

View or assign values to members of the array group separately (it should be noted that, like most programming languages, the subscript of the array member starts counting from 0 ):

$strUsers[0] <enter>

$strUsers[1]="marui" <enter>

You can also use the plus sign to merge arrays, such:

Hash table

A hash table, that is, a dictionary, is a very good data structure that allows us to use keywords for queries. It has almost no performance loss, these are because its basic structure can well meet the computing characteristics of computer storage.

When declaring a hash table variable, you also need to use the symbol "@", such:

$age=@{} <enter> "MaRui"=21;"Lee"=27;"Tom"=53

Operations similar to arrays are as follows:

Copy codeThe Code is as follows: Query
$ Age ["MaRui"] <enter>

Add (Note: assign values when adding values; otherwise, no new entries will be added)
$ Age ["Ma"] = 24 <enter>

Modify
$ Age ["Ma"] = 25 <enter>

Delete (Note: use parentheses)
$ Age. remove ("Ma") <enter>

Clear hash table
$ Age. clear () <enter>

The content of this section is very simple. Thank you for taking the time to browse my blog!

After that, someone asked me how to copy and paste it in PowerShell. In fact, the method is very simple. Right-click it.

Copy

I use a cmdlet to view BIOS information and copy it:

get-wmiobject -class Win32_BIOS <enter>

Step 1:

Use the left mouse button to directly select the content to be copied

Step 2:

Right-click the selected area to copy it.

Paste it here:

SMBIOSBIOSVersion: 7UET82WW (3.12)
Manufacturer: LENOVO
Name: Ver 1.00 PARTTBL (
SerialNumber: R8RNGG3
Version: LENOVO-3120

Paste

Step 1:

Copy the content. Please try copying the following script (this script is used to view the BIOS details ):

Copy codeThe Code is as follows: $ info = get-wmiobject-class Win32_BIOS-namespace root \ CIMV2-comp.
Foreach ($ objItem in $ info ){
Write-host "BIOS Characteristics:" $ objItem. BiosCharacteristics
Write-host "BIOS Version:" $ objItem. BIOSVersion
Write-host "Build Number:" $ objItem. BuildNumber
Write-host "Caption:" $ objItem. Caption
Write-host "Code Set:" $ objItem. CodeSet
Write-host "Current Language:" $ objItem. CurrentLanguage
Write-host "Description:" $ objItem. Description
Write-host "Identification Code:" $ objItem. IdentificationCode
Write-host "Installable versions:" $ objItem. InstallableLanguages
Write-host "Installation Date:" $ objItem. InstallDate
Write-host "Language Edition:" $ objItem. commanageedition
Write-host "List Of versions:" $ objItem. ListOfLanguages
Write-host "Manufacturer:" $ objItem. Manufacturer
Write-host "Name:" $ objItem. Name
Write-host "Other Target Operating System:" $ objItem. OtherTargetOS
Write-host "Primary BIOS:" $ objItem. PrimaryBIOS
Write-host "Release Date:" $ objItem. ReleaseDate
Write-host "Serial Number:" $ objItem. SerialNumber
Write-host "smbios bios Version:" $ objItem. SMBIOSBIOSVersion
Write-host "SMBIOS Major Version:" $ objItem. SMBIOSMajorVersion
Write-host "SMBIOS Minor Version:" $ objItem. SMBIOSMinorVersion
Write-host "SMBIOS Present:" $ objItem. SMBIOSPresent
Write-host "Software Element ID:" $ objItem. SoftwareElementID
Write-host "Software Element State:" $ objItem. SoftwareElementState
Write-host "Status:" $ objItem. Status
Write-host "Target Operating System:" $ objItem. TargetOperatingSystem
Write-host "Version:" $ objItem. Version
Write-host
}

Step 2:

Right-click PowerShell.

Look at the results:

Enter again to execute the command. You can view the result by yourself.

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.