microsoft open course[^1]
# Powershell ISE快捷鍵
control+R show出command line和script的部分
# alias
```=powershell
get-alias
get-alias -Definition get-process # filter and get 'get-process' alias
# 範例
get-alias -d get-service
```
# Get-Member
```=powershell
# Get-Member == gm
```
# man
查詢功能
```=powershell
man get-service # read get-service manual
```
# update-help
更新幫助手冊(記得Force)
```=powershell
update-help -Force
```
# get-help
```=powershell
# 展示一些範例
get-help get-service -Examples
# 顯示詳細內容
get-help get-service -full
# online手冊
get-help get-service -Online
# 另外開一個window的方式查看手冊
get-help get-service -ShowWindow
```
# get-service
```=powershell
# 找關鍵字是bit的service
get-service -Name bits
# 或者
get-service bits
# 找多一點關鍵字
get-service -Name b*, c*
```
# get-eventlog
取得log
```=powershell
get-eventlog # 接著會需要你去輸入需要的log名
# 輸入需要的log名,時間和類型
get-eventlog -LogName System -Newest 3 -EntryType error
# 有加上電腦名稱
get-eventlog -LogName System -Newest 3 -EntryType error -ComputerName dc,s1,s2
```
# 分號 (do and then)
接著做之後的指令
```=powershell
cls ; get-service bits
```
# pipeline (|)
```=powershell
# 輸出csv檔
get-service | export-csv -Path "C:\service.csv"
# 輸出xml
get-process | export-clixml -Path "C:\process.xml"
# write到其他檔案
get-service | out-file -FilePath "C:\here.txt"
# Convert功能
get-xservice | ConvertTo-html -Property name,status
# convert之後輸出file
get-xservice | ConvertTo-html -Property name,status | out-file c:\htmlHere.html
```
# whatif
使用whatif參數會告訴你這樣做的話會發生什麼事情
(特別是你去使用會停止service之類的指令)
```=powershell
get-service | out-file -FilePath "C:\here.txt" -whatif
```
```=powershell
# 確認只有show出的name會停止
get-service -DisplayName *bi* | stop-service -whatif
# 開始有關bi開頭的service
gsv bi* | start-service -verbase
```
# mmc
[ref](https://rishivoice.com/post/1215.html)
microsoft管理控制台
# Get-Module
```=powershell
# 顯示能用的module
get-module -ListAvailable
```
# where
```=powershell
# -gt == great than
get-process | where handles -gt 900
# 其他待確認的
get-service | Where {$_.status = "Running"}
get-service | Where {$PSItem.status -eq "Running" -and $_.name -like "b*"}
```
# select and sort
```=powershell
# 選擇property列的name和length,並且依length排列
get-childitem | select -Property name, length | sort -Property length
```
# object使用
```=powershell
# 取得xml
$x = [xml](cat .\something.xml)
# 顯示 something.xml
$x
# object第一層
$x.PLAY
# object第二層
$x.PLAY.ACT[0].SCENE
# ... 等等
# 添加一些pipeline去group和sort
$x.PLAY.ACT.SCENE.SPEECH | group speaker | sort count
```
# get-history
```=powershell
# 顯示使用過的command
get-history
```
# 使用pipeline去更改內部的property
[ref 21:28](https://docs.microsoft.com/zh-tw/shows/getting-started-with-microsoft-powershell/the-pipeline-deeper)
# 遠端到其他的PC
```=powershell
Enter-PSSession -ComputerName yourPC
```
# show user name
```=powershell
hostname
```
# window連線
```=powershell
start iexplore https://pwa/pswa
```
# 開啟explorer
```=powershell
explorer
```
# param()的使用
[ref](http://jasonyychiu.blogspot.com/2020/02/powershell-param-how-to-pass-parameters.html)
```=powershell
# 自訂參數
# param([TYPE]$varName = defaultValue, ...)
param([String]$name="Lin", $age=26)
```
# Write-Verbose說明
當command的引數多加一個 -verbose時,會印出在
```=powershell
Write-Verbose "This is the verbose information."
```
所寫的內容。
# 用variable當作command使用, 類似alias
```=powershell
$myVar=Get-service -Name b*,c*
# 開始使用
$myVar
# 多個值賦予一個變數, 結果會換行
$servers= 's1', 's2'
# output
# s1
# s2
# 較長的變數
${This is the variable.}="woow"
```
# customized output
```=powershell
# host
write-host "print something on screen"
# warning
write-warning "Please. Don't!"
# error
write-error "It is error message!"
```
# foreach的用法
```=powershell
$servers= 's1', 's2'
# 會去開名字為s1、s2的視窗
$servers | foreach{start iexplore http://$_}
```
[ref](https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/06-flow-control?source=recommendations&view=powershell-7.2)
# @符號建立陣列, hash table 和print出多行string
[ref](https://www.delftstack.com/zh-tw/howto/powershell/use-symbol-in-powershell/)
```=powershell
# 建立陣列
$array = @('a', 'b', 'c')
# output result
$array
# a
# b
# c
# GetType() 獲取 data type
$array.GetType()
# create hash table
$hashTable = @{Name = "Lin"; Age = 99}
# how to use
$hashTable.Name
$hashTable.Age
# print出多行string內容 (有分雙引號跟雙引號)
# 雙引號會印出變數內容
# 單引號則照字面印出
# 但是hashtable的內容不能在這裡使用, 會印出System.Collections.Hashtable.Name
# 單純使用一個字串的變數或數字, 在雙引號會印出內容。
# example:
$myVar = "Lin"
@"
Hi, I am $myVar.
second line
"@
# output
# Hi, I am Lin.
# second line
# 單引號的範例
@'
Hi, I am $myVar.
second line
'@
# output
# Hi, I am $myVar.
# second line
```
# 建立檔案、複製檔案
[ref](https://ithelp.ithome.com.tw/articles/10031897)
New-Item, Copy-Item
```=powershell
# 建立檔案
New-Item -Path C:\\newFile.txt -ItemType "file"
# 複製檔案
Copy-Item -Path C:\\src.txt -Destination C:\\des.txt
# 如果目的的檔案已經存在, 就不能複製, 因此, 需要用force參數去覆蓋原本目的的檔案
Copy-Item -Path C:\\src.txt -Destination C:\\des.txt -Force
```
# 新增資料夾
```=powershell
New-Item -Path "C:\\newDir" -ItemType "directory"
```
# 多個檔案複製到一個資料夾內
```=powershell
Copy-Item -Path "C:\\" -Filter *.txt -Recurse -Destination C:\\des
```
# Test-Path 用法
如果檔案存在回傳true, 反之則false
```=powershell
if( !(Test-Path -Path C:\\exist.txt) ) {
New-Item -Path C:\\createNewFile.txt -ItemType "file"
}
```
# $_ 類似for loop的index功能
```=powershell
1..10 | ForEach-Object -Process {
New-Item -Path "C:\\Dir\file$_.txt" -ItemType "file"
}
```
# CmdletBinding 參考 & 跳出確認框
[ref](https://www.delftstack.com/zh-tw/howto/powershell/powershell-cmdletbinding/)
首先要先寫一個function
```=powershell
# 創建一個function
Function fun_test {
[CmdletBinding(SupportsShouldProcess=$True)]Param()
IF($PSCmdlet.ShouldContinue("sdlkfj", "title") ) {
echo yes
}
}
# 開始執行function
fun_test
# 或是在輸入指令的後面加上confirm
fun_test -Confirm
```
# 多行註解
```=powershell
<#
I want to show you how to do this one.
This is the short explanation.
#>
```
# 顯示環境的位置
```=powershell
$env:PSModulePath
# 上面的指令會用';'隔開, 如果想要分行顯示, 可以使用split
$env:PSModulePath -split ";"
```
[^1]: https://docs.microsoft.com/zh-tw/shows/getting-started-with-microsoft-powershell/
###### tags: `powershell` `powershell ISE` `ISE`