使用PowerShell自动化Windows系统管理任务

1. 管理服务

列出所有服务

Get-Service

启动服务

Start-Service -Name "ServiceName"

停止服务

Stop-Service -Name "ServiceName"


2. 管理计划任务

创建计划任务

$Action = New-ScheduledTaskAction -Execute "C:\Path\To\Executable.exe"
$Trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "MyTask" -Description "Runs at startup."

删除计划任务

Unregister-ScheduledTask -TaskName "MyTask" -Confirm:$false

3. 管理用户账户

创建用户账户

New-LocalUser -Name "UserName" -Password (ConvertTo-SecureString -AsPlainText "Password123" -Force)

修改用户密码

$Password = ConvertTo-SecureString -AsPlainText "NewPassword123" -Force
Set-LocalUser -Name "UserName" -Password $Password

删除用户账户

Remove-LocalUser -Name "UserName"

4. 管理文件和目录

复制文件

Copy-Item -Path "C:\Source\file.txt" -Destination "C:\Destination\file.txt"

移动文件

Move-Item -Path "C:\Source\file.txt" -Destination "C:\Destination\file.txt"

删除文件

Remove-Item -Path "C:\Path\To\File.txt"

5. 管理注册表

获取注册表项

Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion"

设置注册表项

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion" -Name "SomeKey" -Value "SomeValue"

删除注册表项

Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion" -Name "SomeKey"

6. 管理网络

获取网络适配器信息

Get-NetAdapter

设置 IP 地址

Set-NetIPAddress -InterfaceIndex 10 -IPAddress "192.168.1.100" -PrefixLength 24 -DefaultGateway "192.168.1.1"

7. 管理远程系统

连接到远程系统

Invoke-Command -ComputerName "RemoteComputer" -ScriptBlock { Get-Process }

8. 系统信息和监控

获取系统信息

Get-WmiObject -Class Win32_OperatingSystem

监控 CPU 使用率

Get-WmiObject -Class Win32_Processor | Select-Object LoadPercentage


9. 管理 Windows Update

检查更新

Start-Process -FilePath "C:\Windows\System32\wusa.exe" -ArgumentList "-detectnow" -Wait

安装更新

Start-Process -FilePath "C:\Windows\System32\wusa.exe" -ArgumentList "-install" -Wait

10. 执行脚本

创建一个简单的 PowerShell 脚本

# myscript.ps1
Write-Output "Hello, World!"

运行脚本

.\myscript.ps1

11. 更改执行策略

查看执行策略

Get-ExecutionPolicy

更改执行策略

Set-ExecutionPolicy RemoteSigned -Scope Process

12. 创建和使用模块

创建模块文件

# MyModule.psm1
function Get-Hello {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Name
    )
    Write-Host "Hello, $Name"
}

Export-ModuleMember -Function Get-Hello

导入模块

Import-Module .\MyModule.psm1
Get-Hello -Name "World"

13. 使用 PowerShell 任务计划器

创建 PowerShell 脚本任务

$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Path\To\YourScript.ps1"
$Trigger = New-ScheduledTaskTrigger -At 12:00AM
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "RunScriptAtMidnight" -Description "Runs script at midnight."

14. 使用 PowerShell 管道

使用管道连接命令

Get-ChildItem | Where-Object {$_.PSIsContainer} | Format-Table -AutoSize

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小绵羊不怕大灰狼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值