首页 > 系统相关 >(0)powershell常用命令

(0)powershell常用命令

时间:2024-01-18 19:23:17浏览次数:17  
标签:PS Get WINDOWS system32 Weekday 常用命令 字符串 powershell

(0)powershell常用命令

1.PowerShell判断数组里面是否包含指定字符串元素


判断数组是否包含指定元素
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -contains "black"
False

判断数组是否包含指定元素(大小写敏感)
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -ccontains "Green"
False

判断数组是否不包含指定元素
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -notcontains "violet"
True

判断数组是否不包含指定元素(大小写敏感)
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -cnotcontains "Green"
True

2.PowerShell:连接与分隔字符串

一、连接字符串

  • 使用"+"连接字符串:将字符串连接在一起,字符串之间没有分隔符。
PS C:\WINDOWS\system32> $string1="abc" 
PS C:\WINDOWS\system32> $string2="def"
PS C:\WINDOWS\system32> $string3=$string1+$string2
PS C:\WINDOWS\system32> $string3
abcdef

  • "-Join"连接字符串:

语法:

-Join (String1,String2,String3...) 不使用分隔符连接字符串

String1,String2,String3… -Join "Delimiter" 使用分隔符连接字符串

例1:

PS C:\WINDOWS\system32> $a=-Join("abc","def","ghi")
PS C:\WINDOWS\system32> $a
abcdefghi

例2:

PS C:\WINDOWS\system32> $b="abc","def","ghi" -Join ":"
PS C:\WINDOWS\system32> $b
abc:def:ghi
  • 使用"*"运算符:字符串自连接
PS C:\WINDOWS\system32> $string1="abc"
PS C:\WINDOWS\system32> $string2=$string1*3
PS C:\WINDOWS\system32> $string2
abcabcabc

二、分隔字符串

语法:

-Split String 根据空格分隔字符串

String -Split "Delimiter" [,MaxSubStrings] 根据指定分隔符分隔字符串

例1:

PS C:\WINDOWS\system32> $a="abc def ghi"
PS C:\WINDOWS\system32> -Split $a
abc
def
ghi

例2:

PS C:\WINDOWS\system32> $a="abc:def:ghi"
PS C:\WINDOWS\system32> $a -Split ":"
abc
def
ghi

3.PowerShell中的灵活的参数验证功能

1)普通方式:

PS C:\WINDOWS\system32> function Get-Weekday
{
  param
  (
    $Weekday
  )
  "You chose $Weekday"
}

PS C:\WINDOWS\system32> Get-Weekday -Weekday NoWeekday
You chose NoWeekday

2).正则表达式类型的验证方式:
一旦用户输入的字符串与你指定的模式不匹配时,Powershell会抛出一个异常,但是这个异常信息不够友好。在输出参数时,控制台或着ISE编辑器也不能智能提示:

PS C:\WINDOWS\system32> function Get-Weekday
{
param
(
[ValidatePattern('Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday')]
$Weekday
)
"You chose $Weekday"
}

PS C:\WINDOWS\system32> Get-Weekday -Weekday NoWeekday
Get-Weekday : Cannot validate argument on parameter 'Weekday'. The argument "NoWeekday" does not match the "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday" pattern. Supply an argument that matches "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday
|Sunday" and try the command again.
At line:1 char:22
+ Get-Weekday -Weekday NoWeekday
+                      ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Weekday], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-Weekday
 

3)使用ValidateSet:
用户在输出参数时被限定在你规定的值集合中,另外在ISE中还会智能提示用户允许的值列表。

PS C:\WINDOWS\system32> function Get-Weekday
{
param
(
[ValidateSet('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')]
$Weekday
)
"You chose $Weekday"
}

PS C:\WINDOWS\system32> Get-Weekday -Weekday NoWeekday
Get-Weekday : Cannot validate argument on parameter 'Weekday'. The argument "NoWeekday" does not belong to the set "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" specified by the ValidateSet attribute. Supply an argument that is in the set and t
hen try the command again.
At line:1 char:22
+ Get-Weekday -Weekday NoWeekday
+                      ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Weekday], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-Weekday
 

4.查看、执行服务命令

1)查看windows下的所有服务

Get-Service 或者 sc.exe query

2)查看运行中的服务

Get-Service |findstr "Running"

3)查看某个服务状态

Get-Service -ServiceName Dhcp 或者 sc.exe query Dhcp

4)启动某个服务

sc.exe start Dhcp

5)停止某个服务

sc.exe stop Dhcp


5.write-error、write-warning、$host.UI.WriteErrorLine('内容')、$host.UI.WriteErrorLine('内容')

如果你想在PowerShell控制台上输出错误信息或者警告信息,可以分别使用write-error和write-warning这两条命令。它们会采用系统中默认的字体颜色来打印文本。PowerShell会为你的输出采用一个文本模板。

PS C:\WINDOWS\system32> Write-Warning 'Hello,world!'
WARNING: Hello,world!

PS C:\WINDOWS\system32> Write-Error 'You are a big bad egg~'
Write-Error 'You are a big bad egg~' : You are a big bad egg~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

但是Write-Error会引入一个实际的异常,可能会中断脚本的继续运行。如果你只想输出一个看起来像错误的消息,那不妨试试下面这种靠谱一点的写法:
PS C:\WINDOWS\system32> $Host.UI.WriteErrorLine('你是个小坏蛋~')
你是个小坏蛋~

PS C:\WINDOWS\system32> $Host.UI.WriteWarningLine('你是个大坏蛋~')
WARNING: 你是个大坏蛋~

6.文件或文件夹操作

1)删除文件夹及其下所有文件

Remove-Item D:/uu898work/log -Force -Recurse

2)复制文件

Copy-Item d:\ops\win\nssm.exe c:\windows\system32\

标签:PS,Get,WINDOWS,system32,Weekday,常用命令,字符串,powershell
From: https://www.cnblogs.com/zhang-snail/p/17972979

相关文章

  • (7)Powershell算术运算符
    (7)Powershell算术运算符本系列博客从这一节开始是Powershell的语法知识,在开始学习语法之前,希望你对Powershell有个基本的了解,比如开发工具的使用,面向对象等特性,详细内容使劲戳这里(1)-(6)的内容。本节主要介绍Powershell中的算术运算符。Powershell支持以下算术运算符......
  • adb常用命令
    ADB 常用命令ADB命令快速查看屏幕像素密度信息:adbshellwmdensityADB命令快速查看屏幕分辨率信息:adbshellwmsize  分辨率设置命令wmsize[reset|WxH|WdpxHdp]returnoroverridedisplaysize.widthandheightinpixelsunlesssuffixedwith'dp'.查看......
  • (6)Powershell中命令自动补全功能及使用Windows命令
    (6)Powershell中命令自动补全功能及使用Windows命令上一节主要介绍了Powershell中常见的别名,以及怎么通过别名查看真实的Powershell命令,Powershell别名的命名规范以及如何新建自己的别名(Powershell内置别名不可更改)以及Powershell中兼容性别名,详细内容怼介里。在本节主要包含......
  • (5)Powershell别名(Alias)
    (5)Powershell别名(Alias)在上一节,介绍了如何检索当前shell及Powershell中所有可用的命令,对于指定的命令会查看其语法信息,可以获取指定命令的帮助信息,包括获取在线帮助主题,详细内容时间戳这嘎达。在本节中,主要介绍Powershell的别名,主要包含以下内容。熟悉常见的别名。标......
  • (4)Powershell基础知识(二)
    (4)Powershell基础知识(二)上一节主要介绍Powershell可发现,面向对象,一致性等特性,以及Powershell命令是基于.Net对象等重要概念,以及Powershell命令的命名规范,详细内容点击这嘎达。这一节的Powershell基础知识主要包含以下知识点获取命令的摘要信息。获取命令的帮助信息。......
  • (3)Powershell基础知识(一)
    (3)Powershell基础知识(一)上节介绍了Windows自带的Powershell开发工具:命令行行窗体和集成开发环境ISE的启动及一些配置注意事项,具体细节使劲戳Powershell开发工具。这一节介绍Powershell的基础知识,包含以下知识点Powershell的一些特性理解Powershell中的一些重要概念......
  • 如何编写一个 PowerShell 脚本
    PowerShell脚本的后缀是.ps1前提:ps1脚本可以帮忙我们快速修改文件内容,还不需要调用文件的底层api,方便快捷在编写CMakeLists时发现,项目不能够很好的使用vcpkgtoolchain,哪怕是在命令行中指定vcpkg.cmake如果只是简单的项目,vcpkgtoolchain可以正常工作,但是在稍微复......
  • kubectl常用命令
    查看类命令1#获取节点和服务版本信息2kubectlgetnodes3#获取节点和服务版本信息,并查看附加信息4kubectlgetnodes-owide56#获取pod信息,默认是default名称空间7kubectlgetpod8#获取pod信息,默认是default名称空间,并查看附加信息【如:pod的IP及在哪个节点运行】9ku......
  • 在CMD和PowerShell下如何制作图片马
    目录在CMD中使用copy命令:在PowerShell中使用gc命令:总结:图片马通常是在图片文件中嵌入其他信息,以隐藏额外的数据。当使用命令行工具(如CMD或PowerShell)制作图片马时,copy命令和Get-Content(简写为gc)命令的目标是将一段数据(可能是一段脚本或其他二进制数据)嵌入到图片文......
  • DOS窗口常用命令
    |操作       |说明               ||------------------|---------------------------------||盘符名称:    |盘符切换。E:回车,表示切换到E盘。||dir         |查看当前路径下的内容。 ......