Windows PowerShell

Windows PowerShell 是微软发布的为系统管理员设计的基于任务的自动化命令行外壳(Shell)和相关脚本环境,它建立在.NET框架上。 
引入了许多非常有用的新概念,从而进一步扩展了在 Windows 命令提示符和 Windows Script Host 环境中获得的知识和创建的脚本。
==============================================
微软尝试使用各种方法使PowerShell取代目前的CMD
在Windows 7中内置了PowerShell2.0, Windows 8中内置了PowerShell3.0,Windows 10,11中内置了PowerShell5.1

安装PowerShell核心
PowerShell的GitHub页面下载MSI文件。 单击以下链接进行下载:
https://github.com/PowerShell/PowerShell

==============================================
启动Windows PowerShell:
1.在任意文件夹按住 按住 Shift+鼠标右键,选择[ 在此处打开PowerShell窗口(S) ]
2.按Windows + R打开“运行”对话框,然后在文本框中键入“ powershell”。您可以单击“确定”(或按Enter)打开常规的PowerShell窗口
3.由于PowerShell是默认的Windows 10程序,因此可以在“开始”菜单的“所有应用程序”部分单击“ Windows PowerShell”文件夹,然后选择“ Windows PowerShell”将其打开
==============================================
Windows PowerShell 默认值  属性

选项:
光标:小

字体:
大小:24
字体:黑体

 

布局:  --->  窗口大小   ----》 宽度 111, 高度 28 ;
             --->  窗口位置: 取消 “由系统定位窗口”,左88 右88

颜色:
屏幕文字: 红0,绿255, 蓝0
屏幕背景:黑色

==============================================

https://learn.microsoft.com/zh-cn/host-integration-server/core/powershell-module-cmdlets-and-commands

https://zhuanlan.zhihu.com/p/670834959
传统CMD是32位的命令行程序【cmd.exe】,继承DOS操作模式
Cmdlet是PowerShell的命令,所有的cmdlet命令都遵循动词-名词这样语法结构,PowerShell提供了Get-Command这个强大的命令,可以查找所有的cmdlet命令。

powershell中的cmdlet命令
https://www.cnblogs.com/hyyx/p/13935900.html

PowerShell系列:从零开始学习PowerShell系列教程
https://blog.csdn.net/xishining/category_12311171.html

====================================

管理员: Windows PowerShell

====================================

cmdlet

cmdlet发音为:”command-lets”,它是在PowerShell环境中使用的轻量级命令。 这些是在PowerShell环境中实现特殊功能的特殊命令。cmdlet遵循“动词-名词”模式,例如:set-childItem。

 命令:采用口语化的动名词结构  Get-Cmd

自动化运维

在Windows系统中如何查看Powershell的版本
在打开的“powershell“窗口中,输入命令:
$host
或者
Get-Host

PowerShell中的cmdlet执行操作,并将Microsoft .NET Framework对象返回到管道中的下一个命令。 Cmdlet可以接收对象作为输入,也可以将结果输出为对象,这使其适合用作管道中的接收者。

 =========================

Windows PowerShell 集成脚本环境 (ISE) 是 Windows PowerShell 的主机应用程序, ISE代表集成脚本环境。在 Windows PowerShell ISE 中,可以在单一 Windows 图形用户界面中运行命令以及编写、测试和调试脚本,该用户界面具有多行编辑、Tab 补齐、语法着色、选择性执行、上下文相关帮助等功能,而且还支持从右到左书写语言。此主机应用程序最多还可以包括八个会话。 可以自定义 Windows PowerShell ISE 的外观。Windows PowerShell ISE 还有自己的 Windows PowerShell 配置文件,您可以在其中存储在 Windows PowerShell ISE 中使用的函数、别名、变量和命令。

==================================

 PowerShell 脚本文件通常使用 .ps1 作为文件后缀名


Windows PowerShell的默认执行策略受到限制,因此我们无法运行任何脚本,除非对脚本进行更改。(无法加载文件 D:\PowerShellScript\ec2.ps1,因为在此系统上禁止运行脚本

PowerShell中输入以下命令将执行策略设置为Unrestricted以执行脚本
Set-ExecutionPolicy Unrestricted
选择 Y

单行注释 #
get-childitem # 此命令显示C驱动器的子项

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

 =============================

 

tasklist
TASKLIST /? 显示帮助信息
tasklist /S DC01 # 列出在 DC01 上运行的任务
TASKKILL /PID 1230

 

PowerShell 有一套推荐的标准谓词。名词所受限制较少,但它们应始终描述谓词作用的对象。PowerShell 具有 Get-ProcessStop-Process、Get-Service 和 Stop-Service 等命令。

 

10 个谓词和 10 个名词。现在你只需要了解 20 个词。但是这些词可以组合形成 100 个不同的命令名称

 

关闭计算机的命令是 Stop-Computer。列出网络上所有计算机的命令是 Get-Computer。获取系统日期的命令是 Get-Date。

 

Get-Command -Verb Get # 列出包含特定谓词 Get 的所有命令
Get-Command -Noun Service # 查看用于管理服务的命令 同一类型的对象 Noun 产生影响的命令系列

 

创建变量Creating a variable
$loc = Get-Location # 显示当前位置

 $loc | Get-Member -MemberType Property

# 使用 Get-Member 显示有关变量内容的信息。Get-Member 表示 $loc 是 PathInfo 对象,类似于来自 Get-Location 的输出

 ============================================================

Stop-Computer # 关闭计算机
Restart-Computer # 重启操作系统
Restart-Computer -Force # 强制立即重新启动计算机

Get-Service -Name se* # 按名称进行筛选,甚至可以使用星号作为通配符 ( se开头的服务 )
Get-Service -DisplayName se* # 按显示名称查找服务


停止、启动、暂停和重启服务Stopping, Starting, Suspending, and Restarting Services
Stop-Service -Name spooler # 停止打印后台处理程序
Start-Service -Name spooler
Suspend-Service -Name spooler # 暂停打印后台处理程序
Restart-Service -Name spooler # 重启打印后台处理程序

 

Get-Service -name sp*
Stop-Service -Name Spooler
Get-Service -name sp*
Start-Service -Name Spooler

 

=======================================

New-Item -Path c:\temp\hi -ItemType Directory
# 在 C:\Temp 目录中创建名为“hi”的新目录 ; 如果没有Temp目录,也会创建Temp

 

New-Item -Path c:\temp\hi.txt -ItemType Directory # 创建了 hi.txt 目录

New-Item -Path c:\temp\hi.ps1 -ItemType file # 创建名为“hi.ps1”的文件; 如果没有 temp目录,会报错

Rename-Item -Path C:\temp\hi.ps1 my.ps1 # 更改文件的名称
Rename-Item -Path C:\temp ps # 更改文件夹的名称

 ren c:\ps ps2

Move-Item -Path C:\ps\ -Destination C:\ -PassThru # 移动项 (Move-Item)Moving Items (Move-Item)

Copy-Item -Path C:\New.Directory -Destination C:\temp # 复制项 (Copy-Item)Copying Items (Copy-Item)

Remove-Item c:\ps\hi.txt # 删除项 (Remove-Item)Deleting Items (Remove-Item)

Invoke-Item C:\WINDOWS # 效果同 双击 C:\Windows 文件夹一样; 执行项 (Invoke-Item)
Invoke-Item C:\boot.ini # Invoke-Item d:\带表.xlsx 打开文件

 

invoke是一个英语单词,意思是:调用,祈求,恳求,引起

 

##################################################################################

 

# 列出 IP 配置数据Listing IP Configuration Data
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true -ComputerName .

 

# 返回有关 DHCP、DNS、路由以及其他次要 IP 配置属性的详细信息
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*

 

#############################################
使用打印机Working with Printers

 

# 列出计算机 PC-202312261012 上安装的打印机
Get-WmiObject -Class Win32_Printer -ComputerName $env:COMPUTERNAME

 

Get-WmiObject -Class Win32_Printer -ComputerName PC-202312261012

 

# 使用 WScript.Network 添加新的网络打印机
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Printserver01\Xerox5")

 

# 设置默认打印机Setting a Default Printer (在 Win32_Printer 集合中查找打印机,然后调用 SetDefaultPrinter 方法)
(Get-WmiObject -ComputerName . -Class Win32_Printer -Filter "Name='HP LaserJet 5Si'").SetDefaultPrinter()

 

# 删除打印机连接Removing a Printer Connection
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Printserver01\Xerox5")

 

ps -ProcessName QQ*


管理员常用Windows PowerShell命令Top25
https://blog.csdn.net/weixin_30471561/article/details/99490203?

 

############################################################

 

posted @ 2024-05-30 23:33  sangern  阅读(43)  评论(0编辑  收藏  举报