Powershell应用

帮助命令

  • Get-Help
    这个命令用于获取其他命令的帮助文档,例如 “Get-Help Get-ChildItem”。
  • Update-Help
    更新本地帮助文件的版本。
  • Get-Command
    获取系统中所有可用的命令。例如,输入 Get-Command -Module Net 可以获取所有以 “Net” 开头的模块的命令列表。
  • Get-Alias
    获取当前会话中所有的命令别名。例如,输入 Get-Alias -Definition Get-ChildItem 可以获取所有使用 Get-ChildItem 命令的别名。
  • Get-History
    获取当前会话中执行过的命令历史记录。

进程管理

  • Get-Process
    这个命令用于列出正在运行的进程,常用的选项包括“-Name”和“-Id”,例如“Get-Process -Name explorer”。

  • Stop-Process
    这个命令用于终止一个进程,常用的选项包括“-Name”和“-Id”,例如“Stop-Process -Name notepad”。

  • Start-Process
    启动一个新进程。例如,输入 Start-Process -FilePath “C:\Windows\System32\notepad.exe” 可以启动记事本应用程序。

# 启动进程
Start-Process -FilePath "notepad"
Start-Process -FilePath $command -ArgumentList $arguments -RedirectStandardOutput $outputFilePath -RedirectStandardError $errorFilePath -Wait

服务管理

  • Restart-Service
    重启指定的服务。例如,输入 Get-Service 可以获取所有服务列表。

  • Get-Service
    获取系统中所有的服务列表。例如,输入 Start-Service -Name Spooler 可以启动打印机池服务。

  • Stop-Service
    停止指定的服务。例如,输入 Stop-Service -Name Spooler 可以停止打印机池服务。

# 关闭windows update
Stop-Service -Name "wuauserv" -Force
Set-Service -Name "wuauserv" -StartupType Disabled  -ErrorAction SilentlyContinue

文件管理

  • Get-ChildItem
    获取指定目录中的文件和子目录列表。例如,输入 Get-ChildItem -Path “C:\Windows\System32” 可以获取系统 32 文件夹下的所有文件和文件夹列表。

  • Set-Location
    更改当前工作目录。也可以使用其别名“cd”,例如“Set-Location C:\Windows”。

  • Copy-Item
    复制文件或目录。例如,输入 Copy-Item -Path “C:\Temp\file.txt” -Destination “C:\Temp2” 可以将 file.txt 复制到 C:\Temp2 文件夹。

  • Move-Item
    移动或重命名文件或目录。例如“Move-Item C:\Test.txt D:\Archive\Test2.txt”。

  • Remove-Item
    删除文件或目录,常用的选项包括“-Path”和“-Recurse”,例如“Remove-Item -Path C:\Test -Recurse”。

  • New-Item
    创建新文件或目录,常用的选项包括“-ItemType”和“-Path”,例如“New-Item -ItemType Directory -Path C:\Test”。

# 创建文件夹
New-Item -ItemType Directory -Path "C:\path\to\directory"

# 创建文件
New-Item -ItemType File -Path "C:\path\to\file.txt"

# 复制文件
Copy-Item -Path "C:\path\to\file.txt" -Destination "C:\path\to\file_copy.txt"

# 复制文件夹及其内容:
Copy-Item -Path "C:\path\to\directory" -Destination "C:\path\to\directory_dest" -Recurse

# 移动文件
Move-Item -Path "C:\path\to\file.txt" -Destination "C:\path\to\file_new.txt"

# 删除文件
Remove-Item -Path "C:\path\to\file_copy.txt"

# 删除文件夹及其内容:
Remove-Item -Path "C:\path\to\directory" -Recurse

# 列出文件夹内容
Get-ChildItem -Path "C:\path\to\directory"

# 检查文件是否存在
Test-Path -Path "C:\path\to\file.txt"

# 读取文件内容
Get-Content -Path "C:\path\to\file.txt"

# 写入内容到文件
"Content to write" | Set-Content -Path "C:\path\to\file.txt"

# 文件压缩
$sourceDir=""
$destZip=""
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourceDir,$destZip)

# 文件移动
$sourceDir=""
$destDir=""
$extentionList=@("*.jpg","*.log")
if(-not(Test-Path $destDir)){
  New-Item -Path $destDir -ItemType Directory
}

foreach($extention in $ extentionList){
  $partenList=Get-Item -Path $sourceDir -Filter $extention
  $foreach($item in $partenList){
    ReMove-Item -Path $item -Destination $destDir
  }
}

# 文件夹移动
$desktop = "$Env:UserProfile\Desktop"
$txtFolder="$desktop\txt"
$extentionList=@("*.xlsx")
Create-Dir -dir $txtFolder
foreach($extention in $extentionList){
    $files=Get-ChildItem -Path $desktop -Filter $extention
    foreach($file in $files){
       Move-Item -Path $file.FullName -Destination $txtFolder
    }
}
function Create-Dir{
    param([string] $dir)
    if(-not(Test-Path -Path $dir -PathType Container)){
        New-Item -Path $dir -ItemType Directory
    }
}

网络管理

  • Test-Connection
    测试计算机之间的连接状态。

  • Test-NetConnection
    测试网络连接。例如,输入 Test-NetConnection -ComputerName “www.google.com” -Port 80 可以测试计算机是否可以通过端口 80 连接到 Google。

  • Ping
    向指定计算机发送 ICMP 回显请求。

  • Tracert
    显示数据包从本地计算机到指定计算机的路径。

  • Netstat
    显示计算机的网络连接状态和统计信息。

# 测试网络
$connected=Test-Connection -ComputerName baidu.com -Count 1 -Quiet
if($connected){
  Write-Host "connected"
}else{
  Write-Host "not connect"
}

系统管理

  • Get-WmiObject
    获取 Windows 管理信息对象(WMI)的属性和方法。
    用法见PowerShell使用Wmi章节

  • Get-EventLog
    获取指定事件日志的事件列表。

  • Get-ItemProperty
    获取指定注册表键的属性列表。

  • Set-ItemProperty
    设置指定注册表键的属性值。

  • Get-ChildItem Env:
    获取当前会话中所有的环境变量。

用户管理

  • Get-LocalUser
    获取本地计算机上的用户列表。

  • New-LocalUser
    创建新的本地用户。

  • Remove-LocalUser
    删除本地计算机上的指定用户。

  • Add-LocalGroupMember
    将用户添加到本地组。

  • Remove-LocalGroupMember
    从本地组中删除用户。

远程管理

  • Enter-PSSession
    在远程计算机上打开一个 PowerShell 会话。

  • Invoke-Command
    在远程计算机上执行命令。

  • New-PSSession
    创建一个与远程计算机的连接会话。

  • Remove-PSSession
    关闭与远程计算机的连接会话。

  • Enable-PSRemoting
    允许远程计算机上运行的 PowerShell 脚本进行远程管理。

# 1对1 Enter-PSSession
Enter-PSSession -ComputerName xxx.xxx.xxx.xxx -Credential administrator(登录用户)
# 1对多 Invoke-Command
Invoke-Command IP1,IP2,localhost -Command {ipconfig} -Credential Harlan

常见问题

在这里插入图片描述
解决方案:
在这里插入图片描述
Start-Service winrm
winrm quickconfig
Set-Item WSMan:\localhost\Client\TrustedHosts -Value *

字符串和文本处理

  • Select-String
    在文本中查找匹配项。例如,输入 Select-String -Path “C:\Logs*.log” -Pattern “Error” 可以查找 C:\Logs 目录下所有 .log 文件中包含字符串 “Error” 的行。

  • Format-List
    将对象格式化为列表。例如,输入 Get-Process | Format-List Name, Id, CPU 可以将当前正在运行的进程列表格式化为包含名称、ID 和 CPU 列的列表。

  • Split
    将字符串分割为数组。例如,输入 “one,two,three,four” -split “,” 可以将字符串 “one,two,three,four” 分割为包含四个元素的数组。

脚本和模块

  • Set-ExecutionPolicy
    设置脚本执行策略。例如,输入 Set-ExecutionPolicy RemoteSigned 可以设置允许在本地计算机上执行远程签名的脚本。

  • Invoke-Command
    在远程计算机上执行命令。例如,输入 Invoke-Command -ComputerName “remoteComputer” -ScriptBlock { Get-Process } 可以在远程计算机上获取当前正在运行的进程列表。

  • Import-Module
    导入 PowerShell 模块。

  • Export-ModuleMember
    从模块导出命令。

  • New-Module
    创建自定义 PowerShell 模块。

  • Invoke-Expression
    在当前会话中执行字符串作为命令。

其他常用命令

  • Get-Date
    获取当前日期和时间。

  • Get-Random
    生成一个随机数。

  • Measure-Object
    计算对象集合的属性。

  • Out-File
    将输出重定向到文件。

  • Select-Object
    选择对象属性进行显示。

  • Where-Object
    根据条件筛选对象集合。

# 获取桌面
$Env:USERPROFILE\desktop

# 获取脚本所在路径
$scriptDir=Split-Path $MyInvocation.MyCommand.Path

# 空字符串检查
if(-not $mystring){
}

# 获取返回值成员列表
Get-ChildItem|Get-Member
Start-Process -FilePath "notepad" -PassThru |Get-Member

# 最小化powershell
$shell=New-Object -ComObject Shell.Application
$shell.MinimizeAll()

返回值类型

# 获取返回的类型
$result = Get-ChildItem -Path "C:\path\to\directory"
# 在 PowerShell 中,可以使用 GetType() 方法来确定对象的类型。可以将 Get-ChildItem 命令的输出结果存储在一个变量中,并使用 GetType() 方法来查看其类型。
$result.GetType()
# 如果想要详细了解结果的属性和方法,可以使用 Get-Member 命令
$result | Get-Member

PowerShell调用C# 类库

//引入dll
Add-Type –Path "MerRear.dll"

//创建类
$obj = New-Object MerRear.TrallrahurmuSorhardu

//调用方法
$obj.ViwhawSterenekooSirberheeFarzere()

//调用静态方法
[PATool.Core.sys.WmiHelper]::GetUSB()

PowerShell使用Wmi

//获取补丁
Get-WmiObject Win32_QuickFixEngineering

//获取 机器型号 和 制造厂商
Get-WmiObject Win32_ComputerSystem | Format-List Description,Manufacturer,model,UserName

//获取用户已经安装的驱动程序
Get-WmiObject Win32_SystemDriver | Format-List Caption,Name,State,InstallDate,PathName

//获取系统安装软件
Get-WmiObject Win32_Product | Format-List Caption,Description,IdentifyingNumber,InstallDate,InstallLocation,HelpLink,HelpTelephone,InstallSource,Language,LocalPackage,Name,PackageCache,PackageCode,PackageName,ProductID,RegOwner,RegCompany,SKUNumber,Transforms,URLInfoAbout,URLUpdateInfo,Vendor,WordCount,Version

//查找BatteryFullChargedCapacity
Get-WmiObject -Namespace "root\WMI" -Query "select * from BatteryFullChargedCapacity"
Get-WmiObject -Namespace "root\CIMV2" -Query "SELECT * FROM Win32_ProvisioningPackage"

//获取Wmi中的所有类
Get-CimClass -Namespace "root/WMI"Get-WmiObject -Namespace "root\WMI" -List
Get-CimClass -Namespace "root/CIMV2"Get-WmiObject -Namespace "root\CIMV2" -List

WQL测试工具

cmd: wbemtest.exe
获取所有命名空间:Get-WmiObject -Class __Namespace -Namespace root

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

靓仔很忙i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值