[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例
作者:精品下载站 日期:2024-12-14 05:00:59 浏览:13 分类:玩电脑
PowerShell 列表 | PowerShell列表的方法和示例
PowerShell列表简介
PowerShell 列表是一种用于存储项目列表的数据类型或数据结构。列表的大小不是固定的,可以随时更改。列表相对于数组的主要优点是列表可以保存不同数据类型的项,因此它被认为是强数据类型。这存在于系统中。集合点网类。有许多方法和对象与列表关联。本文将详细介绍该列表及其实现以及适当的示例。
语法:
在 PowerShell 中创建列表有两种方法
$testlist = New-Object -TypeName 'System.Collections.ArrayList';
或者
$testlist = [System.Collections.ArrayList]::new()
空列表可以定义为
[System.Collections.ArrayList]$testlist= @()
例如。:
输入:
Write-Host "Demo of list in Powershell"
$testlist = New-Object -TypeName 'System.Collections.ArrayList'
Write-Host "Datatype is " $testlist.GetType() -ForegroundColor Green
$testlist1 = [System.Collections.ArrayList]::new()
Write-Host "Datatype is " $testlist1.GetType() -ForegroundColor Green
Write-Host "Declaring an empty list"
[System.Collections.ArrayList]$testlist2= @()
Write-Host "Datatype is " $testlist2.GetType() -ForegroundColor Green
输出:
Demo of list in Powershell
Datatype is System.Collections.ArrayList
Datatype is System.Collections.ArrayList
Declaring an empty list
Datatype is System.Collections.ArrayList
可用于 ArrayList 的方法
以下是一些可用于列表的方法。它们是 add、addrange、binarysearch、clear、clone、contains、copyto、equals、getenumerator、gethashcode、getrange、gettype、insertrange、setrange、removerange、remove、removeat、sort、toarray、tostring、where、trimtosize、item 和 foreach。
以下是属性:capacity、count、isfixedsize、isreadonly、issynchronized、syncroot。
方法及使用
Add():该方法用于向列表中添加一个元素
Clear():该方法用于清空列表内容
Contains():此方法用于检查列表中是否存在某个项目
Sort():该方法用于对列表中项目的元素进行排序
Remove():此方法删除指定值的元素
RemoveAt(): 此方法删除指定位置处的元素。
Clone():此方法创建列表的副本
CopyTo():该方法用于将一个列表的内容复制到另一个列表
InsertRange():该方法向列表中指定范围内插入一个新元素
TrimtoSize(): 该元素将列表缩小到指定大小
Foreach():该方法用于循环
GetType():该方法用于查找列表的类型
RemoveRange():此方法删除指定范围内的项目列表
ToArray():此方法将列表转换为数组
PowerShell 列表示例
下面给出了 PowerShell 列表的示例:
例子#1
将项目添加到列表
输入:
Write-Host "Demo of adding items to a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
Write-Host "the items in the list are" -ForegroundColor Green $dlist
输出:
例子#2
从列表中删除项目
输入:
Write-Host "Demo of removing items from a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
$dlist.Add("Vyapini")
$dlist.Add("Vijay")
$dlist.Add("VJS")
$dlist.Add("VVA")
Write-Host "the items in the list are" -ForegroundColor Green $dlist
Write-Host "The count of items in the list is " $dlist.Count
$cin= Read-Host "Enter the position of the element to be deleted"
$dlist.RemoveAt($cin)
Write-Host "Now the items in the list are" $dlist -ForegroundColor Green
$cin= Read-Host "Enter the element to be deleted"
$dlist.Remove($cin)
Write-Host "Now the elements in the list are" -ForegroundColor Green $dlist
Write-Host "Demo of deleting multiple items"
$index= Read-Host "enter the index from where it has to be deleted"
$range= Read-Host "Enter the number of items to be deleted"
$dlist.RemoveRange($index,$range)
Write-Host "Now the elements in the list are" -ForegroundColor Green $dlist
输出:
例子#3
对列表中的项目进行排序和循环
输入:
Write-Host "Demo of sorting items in a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
$dlist.Add("Vyapini")
$dlist.Add("Vijay")
$dlist.Add("VJS")
$dlist.Add("VVA")
Write-Host "the items in the list are" -ForegroundColor Green $dlist
Write-Host "Sorting the items in the list in ascending order" -ForegroundColor Green
$dlist |Sort-Object
Write-Host "sorting the items in the list in desceding order" -ForegroundColor Green
$dlist |Sort-Object Descending
Write-Host "looping the list items" -ForegroundColor Green
foreach($d in $dlist)
{
Write-Host "The current element is" $d
}
输出:
例子#4
输入:
Write-Host "Demo of various methods in a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
$dlist.Add("Vyapini")
$dlist.Add("Vijay")
$dlist.Add("VJS")
$dlist.Add("VVA")
Write-Host "count of the list is:" -ForegroundColor Green $dlist.Count
Write-Host "capacity of the list is:" -ForegroundColor Green $dlist.Capacity
Write-Host "is the list synchronized:" -ForegroundColor Green $dlist.IsSynchronized
Write-Host "is the list of fixed size" -ForegroundColor Green $dlist.IsFixedSize
write-Host "Is the list read-only" -ForegroundColor Green $dlist.IsReadOnly
write-Host "Is the list synced" -ForegroundColor Green $dlist.SyncRoot
$dlist.AddRange(@("a","b"))
Write-Host "The elements in the list now are" $dlist -ForegroundColor Green
Write-Host "Cloning the list" -ForegroundColor Green
$dlistclone=$dlist.Clone()
Write-Host "Elements in clone are" $dlistclone -ForegroundColor Green
Write-Host "clearing the contents of the list" -ForegroundColor Green
$dlistclone.Clear()
Write-Host "size of the clone list" $dlistclone.Count -ForegroundColor Green
Write-Host "check if the item is there in the list" -ForegroundColor Green
$toc= Read-Host "enter the item to be checked" -ForegroundColor Green
$dlist.Contains($toc)
$dlist1 = New-Object -TypeName 'System.Collections.ArrayList';
$dlist1.Add("100000")
$dlist1.Add("999999")
Write-Host "copying list items from one list to another" -ForegroundColor Green
$dlist1.CopyTo($dlist)
Write-Host "new values in dlist1:" $dlist -ForegroundColor Green
Write-Host "checking two lists" -ForegroundColor Green
$dlist.Equals($dlist1)
Write-Host "getting range" -ForegroundColor Green
$dlist.GetRange(1,2)
Write-Host "inserting a value at the specified position" -ForegroundColor Green
$ind= Read-Host "enter the index at which the item has to be inserted"
$val= Read-Host "enter the value"
$dlist.Insert($ind,$val)
Write-Host "now the values of the list " $dlist -ForegroundColor Green
Write-Host "reducing the size of the list" -ForegroundColor Green
$siz= Read-Host "enter the size of the list to be"
$dlist.TrimToSize()
Write-Host "current length of the list is" -ForegroundColor Green $dlist.Count
输出:
结论
因此,本文详细介绍了 PowerShell 中的列表。它详细解释了与列表相关的各种属性和方法、其用法和类型,以及适当的示例。要了解有关 PowerShell 列表的更多信息,建议编写示例脚本并进行练习。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag