Windows Powershell command returns an array _powershell

Source: Internet
Author: User

When we save the execution result of a command to a variable, we might think that the variable is stored in plain text.
However, in fact PowerShell will save the text as an element in each row as an array. If the return value of a command has more than one result, PowerShell automatically stores the result as an array.

PS c:powershell> $IPcfg =ipconfig
PS c:powershell> $IPcfg

Windows IP Configuration
Ethernet Adapter Local area Connection:

  connection-specific DNS Suffix.: * * *
  link-local IPv6 Address ...: * * *
  IPv4 ADD Ress ...... : 192.168.140.128
  Subnet Mask ...: 255.255.252.0
  Default Gateway .........:192.168.14. 0.1

Tunnel Adapter isatap.mossfly.com:

  connection-specific DNS Suffix.: * * *
  link-local IPV6 Address ...: * * *
  Default Gateway ...:* * *

tunnel Adapter Teredo Tunneling Pseudo-Interface:

  M Edia State ........... : Media disconnected
  connection-specific DNS Suffix.:
PS c:powershell> $IPcfg. Count
22

Using arrays to store results
Determines whether a variable is an array

PS c:powershell> $ip =ipconfig
PS c:powershell> $ip-is [array]
True
PS c:powershell> "Abac"-is [ Array]
False
ps c:powershell> $str = "string"
PS c:powershell> $str. ToCharArray ()-is [array]
True

View the number of elements in an array with $array. The Count property. Accesses the X element, using $array[x-1] because the array is indexed at 0.

Use the pipeline to further handle the array

PS c:powershell> ipconfig | Select-string "IP"

Windows IP Configuration
  link-local IPv6 Address ...: * *
  IPV4 Address . . . . . . . . . . . : * * *
  link-local IPv6 Address ...: * * *

Using real object manipulation

Why would you not call the result of a ipconfig return an object, because it is not a true cmdlet command, the real PowerShell command returns an array element that can be more than one string, and is a rich object.

PS c:powershell> ls directory:c:powershell Mode lastwritetime Length Name------------------- --------D----2011/11/23 17:25 ABC d----2011/11/29 18:21 myscript-a---2011/11/24 18:30 675   A.html-a---2011/11/24 20:04 26384 a.txt-a---2011/11/24 20:26 12060 alias-a---2011/11/24 20:27 12060 alias.ps1-a---2011/11/23 17:25 0 b.txt-a---2011/11/23 17:25 0 c.txt-a---2011/11/23 17    : 0 d.txt-a---2011/11/25 11:20 556 employee.xml-a---2011/11/29 19:23 21466 function.ps1-a---     2011/11/28 11:12 186 logotestconfig.xml-a---2011/11/24 17:37 7420 name.html-a---2011/11/28 15:30 
Ping.bat-a---2011/11/24 17:44 735892 powershell_cmdlets.html-a---2011/11/30 16:04 2556 psdrive.html A---2011/12/2 18:47 140 test.ps1-a---2011/11/23 17:37 242 test.txt-a---2011/11/28 16:42 17 0 Test.vbs PS C:powershell> $result =ls PS c:powershell> $result.
 Count 20

Each element of the


Array holds a System.IO.DirectoryInfo object.
When we output these objects, PowerShell automatically converts it to a friendly text format.

PS c:powershell> $result [0].gettype (). FullName System.IO.DirectoryInfo PS c:powershell> $result [0] Directory:c:      Powershell Mode lastwritetime Length Name---------------------------d----2011/11/23 17:25

ABC can use format-list * to view all of its properties and methods for any object. PS c:powershell> $result [0] | FL * PSPATH:MICROSOFT.POWERSHELL.COREFILESYSTEM::C:POWERSHELLABC PSParentPath:Microsoft.PowerShell.CoreFileSys   Tem::c:powershell pschildname:abc psdrive:c PSProvider:Microsoft.PowerShell.CoreFileSystem Psiscontainer : True basename:abc mode:d----name:abc Parent:powershell exists:true root:c : Fullname:c:powershellabc extension:creationtime:2011/11/23 17:25:53 creationtimeutc:2011/11/23 9:25:5 3 lastaccesstime:2011/11/23 17:25:53 lastaccesstimeutc:2011/11/23 9:25:53 lastwritetime:2011/11/23 17:25:53 LastW
 RITETIMEUTC:2011/11/23 9:25:53 Attributes:directory

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.