PowerShell

出自ProgWiki
於 2022年8月11日 (四) 13:04 由 Player討論 | 貢獻 所做的修訂 →‎計算檔案的雜湊值
跳至導覽 跳至搜尋

PowerShell,參照:『維基百科~PowerShell

技術文件

其他

應用

其他

FAQ

安裝PowerShell

常用指令

  • Get-Help
取得指令的使用說明
  • exit
離開PowerShell
  • HELP
線上說明
  • kill
停止執行中的程序
  • ps
顯示執行中的程序狀態
  • Get-Module
顯示已安裝的模組
  • Get-Package
顯示已安裝的軟體套件
  • Find-Package -Name 套件名
尋找套件(需安裝Nuget)
  • Get-PackageSource
顯示能安裝的軟體套件的來源

軟體套件安裝

經由Chocolatey

$ Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  • 安裝軟體 (以notepadplusplus為例)
$ choco install notepadplusplus

從WindowsApps安裝應用軟體

  • 以WindowsCalculator為例
$ManifestPath = (Get-AppxPackage -Name "*WindowsCalculator*").InstallLocation + "\Appxmanifest.xml"
Add-AppxPackage -Path $ManifestPath -Register -DisableDevelopmentMode

改善PowerShell的效能

取得網路卡資訊

Get-WmiObject win32_NetworkAdapter | %{ if ($_.PhysicalAdapter -and $_.Name -notmatch "Virtual") {$_ } }

使用7z

使用git

使用xml

取得資料夾內附檔名為zip的檔名

$Dir="D:/Folder"    
 
foreach($item in (dir $Dir "*.zip")){ 
    Echo $item.Name
}

關閉程式與執行程式

關閉程式
#取得授權
Set-ExecutionPolicy RemoteSigned
 
#判斷與停止Client
if(Get-Process client -ErrorAction SilentlyContinue)
{
    Stop-Process -Name client
}
 
#等待1s
Start-Sleep -s 1
執行程式
#取得授權
Set-ExecutionPolicy RemoteSigned
#執行項目
[System.Diagnostics.Process]::Start("C:\Client\client.exe", "")

刪除超過30天的資料庫備份檔

# 清除超過30天的資料庫備份檔
#
# 用法:
# 用管理員身分執行 PowerShell (怕資料夾或檔案鎖在管理員權限而無法刪除)
# >D:
# >CD D:\Buckup
# >./ClearDbBackup.ps1
 
 
#定義主目錄
#透過Get-ChildItem抓該資料夾內的檔案列表
$TargetFolder = "D:\Backup\DB"
 
#定義天數,要刪除多久以前的檔案
#若是要刪除30天前的檔案,$Days就設定30 
$Days = 30
 
#取今日時間
$Currentlytime = (Get-Date).AddDays(-$Days)
 
$Dirs = Get-Childitem -Path $TargetFolder -Recurse -Include *.bak
 
foreach ($item in $Dirs) 
{
	#判斷檔案最後修改時間
	#由LastWriteTime抓取檔案最後修改時間
	#利用AddDays對$Currentlytime做時間的運算 
	if($item.LastWriteTime -le $Currentlytime)            #-le 小於或等於
	{
	        Remove-Item $item.FullName | out-null  
        	write-host "Finish Deleting Dir $item" -ForegroundColor "DarkRed"
	}    
}

ASP.NET WebForm PostBack

JSON

批次轉檔

文字亂碼

計算檔案的雜湊值

圖檔處理

其他

安裝
啟動方法
  • 【開始】→【執行】→【powershell】