PowerShell -- Command

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

PowerShell 命令称为 cmdlet(读作 command-let)
可以用任何编译的 .NET 语言或 PowerShell 脚本语言本身来编写 cmdlet。
它是在PowerShell环境中使用的轻量级命令。 这些是在PowerShell环境中实现特殊功能的特殊命令。cmdlet遵循“动词-名词”模式,例如:set-childItem----------------------------------------
start-process PowerShell -verb runas	#将以管理员身份提升Windows PowerShell的权限。

注释:
	单行注释:#
	多行注释:<# .....
				....... #>

帮助命令:
Get-Help cmdlet或 help 函数
	功能一致。
	1.帮助你了解找到命令后如何使用它们
	2.帮助查找命令
		首先根据提供的输入来搜索命令名称的通配符匹配项。 
		如果找不到匹配项,它将搜索帮助主题本身;
		如果还找不到匹配项,则返回错误。
		如果只找到一个结果,将显示帮助主题本身,而不显示命令列表。
	参数:
		-Name		#结合通配符查找
					#eg:	help *process*
		-Full		#可返回整个帮助主题
					#eg:	help Get-Help -Full
		-Parameter	#仅返回带有指定参数名的的帮助主题中的信息 
					#eg:	help Get-Help -Parameter Name
Get-Command cmdlet
	帮助查找命令
	运行不带任何参数的 Get-Command 会返回系统上所有命令的列表。
	参数:
		-Name 
		
Update-Help cmdlet 
	用于更新帮助主题。 默认情况下,它需要访问 Internet,并且你需要以管理员身份运行提升的 PowerShell。

Get-Member	cmdlet 
	可帮助发现可用于命令的对象、属性和方法。 任何生成基于对象的输出的命令都可以通过管道传递到 Get-Member




----------------------------------------
常用自动变量:
#						#注释
$PSVersionTable			#显示相关 PowerShell 版本信息的哈希表




常用命令:
Get-ExecutionPolicy		#检查当前的执行策略 	建议使用 RemoteSigned 策略,该策略要求下载的脚本必须由受信任的发布者签名才能运行。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned		#更改执行策略为 RemoteSigned 模式
Out-GridView			#将结果输出到一个新起的PowerShell界面		
*					 	#通配符,加到开头或结尾,中间无效,类似sql的'%123%',cmdlet默认情况加自动加此通配符		
						#eg: help *process*  查找帮助中name包含 process 的行
-						#后面紧跟参数名称,eg:help -process 查找的是以 -process 结尾的命令	
			  

powershell cmdlets 不区分大小写

基本powershell命令:

	1.Get-ChildItem.此cmdlet不会显示空目录
		②.别名:gcidirls.参数
				-Recurse		递归,获取所有子容器中的项目
				-Depth			限制要递归的级别数
								eg: ls -depth 0   #获取当前目录下文件信息
								
				-Attribute		获取具有指定属性的文件和文件夹,它支持所有属性,并允许您指定属性的复杂组合。
								支持以下属性(可缩写):D (Directory),H (Hidden),R (Read-only),S (System) ,Archive, Device, Encrypted, IntegrityStream,NotContentIndexed, Normal, Offline, NoScrubData, ReparsePoint, Compressed, System, SparseFile, and Temporary。
								运算符组合属性:! (NOT)	+ (AND)	, (OR)
								eg:	ls -Attribute h+r   #查看当前文件夹下属性为隐藏且只读的文件或文件夹
								
				-Directory		用于获取当前文件夹下目录列表,可配合递归-Recurse使用
								eg:	ls -directory  等同于: ls -attribute d
								
				-File			用于获取文件列表,可配合递归-Recurse使用
								eg:	 ls -file -recurse -depth 1
				
				-Name			检索位置中项目的名称
								eg:	ls -name 02*		
				
				#-Path			用于指定一个或多个位置的路径。默认位置是当前目录.,允许使用通配符。
								多个路径时,中间用,隔开
								eg:	ls -path  '.\03 BurpUnlimited\',.\20220422API\
								
				#-Exclude		指定cmdlet从操作中排除的那些项目,配合通配符 * 使用
								类似sql的 not like
								eg:	ls -directory -exclude 03*  
								
				#-Include		指定cmdlet从操作中包含的项目
				
				-Filter			用于限定参数的过滤器
								在cmdlet检索前应用此过滤器
								eg:	ls -directory -filter 03*	#查找目录中名称包含03的目录
				#-Hidden			用于获取隐藏的项目
				
				#-FollowSymlink	用于搜索以这些符号链接为目标的目录。
								eg:	没找到实用例子


	2.Get-Item
		①在特定位置获取项目。 除非使用*通配符来请求项目的所有内容,否则此cmdlet不会在指定位置获取项目的内容。
		eg:  Get-Item C:\*
			 Get-Item .\*
		②参数
				-Path			指定项目的路径,接受通配符。
								eg:	Get-Item -Path C:\*
			 						Get-Item -Path .\*
			 	#-LiteralPath	和-Path区别是不解释通配符。不解释任意转义字符	
			 					eg:	Get-Item -LiteralPath  03*    			
				#-Force			指示用户无法访问的那些项目.
				#-Include				
				-Filter
				#-Exclude
				#-Stream			从文件中获取特定的备用NTFS文件流,支持通配符。


	3.Get-Location
		①获取有关当前工作目录或位置堆栈的信息。
		②等同于 pwd
		③参数
				#-PSDrive		指定此cmdlet在操作中获得的给定PowerShell驱动器中的当前位置
								eg:	Get-Location -PSDrive E
				#-PSProvider		用于指定此cmdlet在操作中获得的PowerShell提供程序支持的驱动器中的当前位置。
				#-Stack			用于显示当前位置堆栈中的位置。
				#-StackName		用于显示不同位置堆栈中的位置。

	4.Set-Item
		①将项目的值更改为命令中指定的值。
			它更改变量或注册表项的值。
		②参数
				-Value			用于为项目指定新值。
				-Confirm		在运行cmdlet之前提示确认。
				#-Path
				#-LiteralPath
				#-Force			允许cmdlet设置无法更改的项目,例如变量或只读别名。但无法覆盖安全权限。
				#-PassThru		用于将表示项目的对象传递到管道。 默认情况下,set-item不会生成任何输出。
				#-Filter			
				#-Exclude
				#-Include
				
	5.Set-Location
		①将当前PowerShell的工作位置设置为指定位置。 
			该位置可以是目录,注册表位置,子目录或任何提供程序路径。
		②别名:sl,cd ,chdir
		③参数
				-Path
								eg:	set-location -path "HKLM:"
									set-location E:
				-LiteralPath
				-PassThru
				-StackName

	6.New-Item
		①创建文件和文件夹,注册表中创建注册表项和条目。
			 创建文件后,它还可以将初始内容添加到文件中。
		②参数
				-path			用于指定新文件或文件夹的位置的路径,接受通配符。
				-ItemType		指定新项目的指定提供者的类型。
								文件系统驱动器中,则允许使用这五个值(File,SymbolLink,Directory,Junction,HardLink)。
								认证驱动器中,则可以指定以下值:证书提供者,证书,商店,商店位置。
				-Name			指定新文件或文件夹的名称。
				-Value			用于表示新项目的值。
				-Force			强制此cmdlet创建覆盖现有只读项目的项目。
				-WhatIf			描述了如果执行cmdlet将会发生的情况,该cmdlet不执行。
				-Confirm		在执行cmdlet之前提示进行确认。
		
	7.Copy-Item
		①将项目从一个位置复制到名称空间中的另一个位置。
			可以复制文件系统驱动器中的文件和目录以及注册表驱动器中的注册表项和条目。
			可以复制和重命名同一cmdlet中的项目。
		②参数
				-Destination	此参数用于指定新位置的路径 或 要重命名的项目名称。
								eg:	 copy-item "file.txt" -Destination "D:\mydir"	#将file.txt文件复制到D:\mydir目录。 原始文件(file.txt)不会被删除。
									 copy-item -path "E:\xntutor" -destination "D:\workspace"	#复制E:\xntutor目录中的内容到一个新创建的目录:D:\workspace
									 copy-item hah.txt -destination a.txt	#重命名文件
				-Confirm		
				-Path		
				-LiteralPath
				-Filter
				-Force
				-Recurse
				-Include
				-Exclude
				-Container		此参数指示此cmdlet在复制操作期间保留容器对象。 默认情况下,此参数设置为True。
				-PassThru
				-Credential		与PowerShell一起安装的任何提供程序均不支持-Credential参数。 默认情况下,此参数使用当前用户。
				-WhatIf
				-FromSession	此参数用于指定从中复制远程文件的PSSession对象。使用它时,-path和-LiteralPath参数都引用远程计算机上的本地路径。
				-ToSession		此参数用于指定要将远程文件复制到的PSSession对象。 当使用它时,-Destination参数引用远程计算机上的本地路径。


	8.move-item
		①将项目从一个位置移动到另一位置。或者将注册表子项从一个键移动到另一个键。
		②别名:mimvmove
		③参数
				-Path			指定项目当前位置的路径,接受通配符。
				#-LiteralPath	
				-Destination	用于指定复制项目的位置的路径。可指定一个新名称,以重命名要移动的项目。
				-Confirm		提示确认
				#-Force
				#-Filter
				#-Include
				#-Exclude
				#-PassThru
				#-WhatIf
				
				eg:	move-item -path "C:\xntutor\readme.txt" -destination "D:\powershell\readme.txt"	#将将文件移动到另一个目录
					move-item -path "C:\xntutor\readme.txt" -destination "D:\powershell\readme-new.txt"	#将文件移动到另一个位置并重命名该文件
					move-item -path "C:\xntutor" -destination "D:\powershell"	#将指定的目录及其内容移动到另一个目录
					
		
	9.Remove-Item
		①删除一个或多个指定项目。
			包括文件,文件夹,变量,注册表项,别名和函数。	
		②参数
				-Path			指定项目当前位置的路径,接受通配符。
				#-LiteralPath	
				-Destination	用于指定复制项目的位置的路径。 可以使用通配符,但是输出必须指定一个位置。
				-Confirm		提示确认
				#-Force
				#-Filter
				#-Include
				#-Exclude
				#-PassThru
				#-WhatIf
				
				eg:	remove-item text.txt						#删除当前目录中的指定文件
					remove-item "E:\xntutor\powershell\*.txt"	#删除以.txt扩展名的所有文件
					move-item * -Include *.doc -Exclude *xn*	#从当前文件夹中删除所有扩展名为.doc且名称不包括xn的文件。
	
	10.Rename-Item		
		①更改指定项目的名称。 不影响被重命名的项目的内容。
		②别名:rirmrmdirdel,ease和rd
		③参数
				-Path			指定要重命名的项目的路径
				-LiteralPath
				-NewName		指定项目的新名称,不能在此参数中使用通配符。
				-Confirm		提示确认	
				#-Force
				#-PassThru
				#-WhatIf
				
				eg:	rename-item readme.txt -newname new-readme.txt									#将文件readme.txt重命名为new-readme.txt。
					get-childitem *.txt | rename-item -newname {$_.name -replace '.txt', '.doc'}	#将所有.txt文件重命名为.doc文件。

	11.Add-Content
		①将内容附加到指定的项目或文件。
			可以通过在cmdlet中键入内容或指定包含内容的对象来指定内容。
		②参数
				-Path			
				-LiteralPath	
				-Destination	
				-Confirm		提示确认。	
				-Encoding		指定目标文件的编码类型。默认值为UTF8NoBOM
				-Value			指定要添加的内容。可以使用Get-content来获取内容并将其传递给-Value参数。
				#-Force			
				#-Filter	
				#-Include
				#-Exclude
				#-PassThru 
				#-WhatIf 		
				#-NoNewLine		此cmdlet不会向内容添加新行或回车。
				#-Stream			为内容指定备用数据流。如果流不存在,它将创建它。参数接受通配符。
				eg:	add-content newfile.txt -value "这是一个测试添加内容"			#将字符串添加到指定文件
					add-content .\*.txt -value "这是一个测试添加内容(多个文件)"	#将字符串添加到所有文本文件
					add-content newfile2.txt -value (get-date)					#在指定文件的末尾添加一个日期
					add-content newfile2.txt -value (get-content file2.txt)		#创建一个新文件并复制内容
					
	12.Clear-Content
		①删除项目的内容,但不删除该项目
		②别名:clc
		③参数
				-Path
				-Confirm 	
				#-LiteralPath
				#-Destination
				#-Force
				#-Filter
				#-Include
				#-Exclude
				#-WhatIf			
				eg:	clear-item -path "E:\xntutor\powershell\MyFile.txt"		#从指定文件中删除(清空)内容.该文件未删除,但文件内容为空。
					clear-item -path ".\*.txt"								#删除指定目录中所有.txt文件的内容。
		
	13.Get-Content
		①在指定位置获取项目的内容
		②别名:gccattype
		③参数
				-Path
				-Confirm 
				-ReadCount		指定一次通过管道发送多少行内容。
				-TotalCount		指定从文件开头开始的行数。
				-Tail			指定从文件末尾开始的行数。
				-Encoding		用于指定目标文件的编码类型。
				-NoNewLine		指示此cmdlet不会向内容添加新行或回车。
				#-LiteralPath
				#-Force
				#-Filter
				#-Include
				#-Exclude
				#-WhatIf
				...			
				eg:	get-content file1.txt								#获取文本文件的内容
					get-content file2.txt -tail 2						#显示文件中内容的最后2行
					get-content file2.txt -totalcount 6					#显示内容的前6行。
	
	14.Get-Date

















  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
v7.0.0-preview.4 - 09/19/2019 Engine Updates and Fixes Add support to ActionPreference.Break to break into debugger when Debug, Error, Information, Progress, Verbose or Warning messages are generated (#8205) (Thanks @KirkMunro!) Enable starting control panel add-ins within PowerShell Core without specifying .CPL extension. (#9828) Performance Make ForEach-Object faster for its commonly used scenarios (#10454) and fix ForEach-Object -Parallel performance problem with many runspaces (#10455) Experimental Features Update PSDesiredStateConfiguration module version to 2.0.3 and bring new tests; enable compilation to MOF on non-Windows and use of Invoke-DSCResource without LCM (#10516) Add APIs for breakpoint management in runspaces and enable attach to process without BreakAll for PowerShell Editor Services (#10338) (Thanks @KirkMunro!) Support ternary operator in PowerShell language (#10367) General Cmdlet Updates and Fixes Add PowerShell Core group policy definitions (#10468) Update console host to support XTPUSHSGR/XTPOPSGR VT control sequences that are used in composability scenarios. (#10208) Add WorkingDirectory parameter to Start-Job (#10324) (Thanks @davinci26!) Remove the event handler that was causing breakpoint changes to be erroneously replicated to the host runspace debugger (#10503) (Thanks @KirkMunro!) Replace api-ms-win-core-job-12-1-0.dll with Kernell32.dll in Microsoft.PowerShell.Commands.NativeMethods P/Invoke API(#10417) (Thanks @iSazonov!) Fix wrong output for New-Service in variable assignment and -OutVariable (#10444) (Thanks @kvprasoon!) Fix global tool issues around exit code, command line parameters and path with spaces (#10461) Fix recursion into OneDrive - change FindFirstFileEx() to use SafeFindHandle type (#10405) Skip auto-loading PSReadLine on Windows if the NVDA screen reader is active (#10385) Increase built-with-PowerShell module versions to 7.0.0.0 (#10356) Add throwing an error in Add-Type if a type with the same name already exists (#9609) (Thanks @iSazonov!)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值