ทำไมต้อง PowerShell
- ถ้าใช้ Windows ใช้ Powershell ได้เลย ไม่ต้องติดตั้งเพิ่มเติม
แนะนำให้ใช้ Command Prompt แทน PowerShell แทน เนื่องจากมันดีกว่าและสมัยใหม่กว่า
Usage
ใช้ใน Windows Terminal
ใช้ใน Wezterm
sh
-- config ของผม
local wezterm = require 'wezterm'
wezterm.on("gui-startup", function(cmd)
local tab, pane, window = wezterm.mux.spawn_window(cmd or {})
--- pane:split { direction = "Right" }
window:gui_window():maximize()
end)
return {
default_cwd = 'D:\\wrikka\\learn-wrikka-com',
default_prog = {"pwsh"},
font = wezterm.font("MesloLGL Nerd Font Mono"),
font_size = 12.0,
color_scheme = "Gruvbox Dark",
hide_tab_bar_if_only_one_tab = true,
default_cursor_style = "SteadyBar",
enable_scroll_bar = true,
keys = {
{key = "t",mods = "CTRL",action = wezterm.action { SpawnTab = "CurrentPaneDomain" }},
{key = "n",mods = "CTRL",action = wezterm.action { SplitHorizontal = { domain = "CurrentPaneDomain" } }},
{key = "w",mods = "CTRL",action = wezterm.action { CloseCurrentTab = { confirm = false } }},
{key = "v",mods = "CTRL",action = wezterm.action { PasteFrom = "Clipboard" }},
{key = "c",mods = "CTRL|SHIFT",action = wezterm.action { CopyTo = "Clipboard" }},
{key = "c",mods = "CTRL",action = wezterm.action_callback(function(window, pane)
if window:get_selection_text_for_pane(pane) == "" then
window:perform_action(wezterm.action { SendString = "\x03" }, pane)
else
window:perform_action(wezterm.action { CopyTo = "Clipboard" }, pane)
end
end)},
{key = "f",mods = "CTRL",action = wezterm.action.Search { CaseInSensitiveString = "CurrentSelectionOrEmptyString" }},
{key = "a",mods = "CTRL",action = wezterm.action { SendString = "\x01" }}
}
}
ใช้ใน VS Code
เมื่อกำหนด "terminal.integrated.defaultProfile.windows": "PowerShell"
ใน settings.json => แล้วใน Terminal สามารถกดเลือก Shell ได้
Command
ดูคำสั่งทั้งหมด => พิมพ์ Get-Command
คำสั่งที่ใช้งานบ่อยๆ => พิมพ์ Get-Alias
Alias ที่ใช้งานบ่อย
คำสั่ง | คำอธิบาย |
---|---|
ls | แสดงรายการไฟล์และโฟลเดอร์ในไดเรกทอรีปัจจุบัน |
ni example.txt | สร้างไฟล์ใหม่ชื่อ "example.txt" |
md MyFolder | สร้างโฟลเดอร์ใหม่ชื่อ "MyFolder" |
rm example.txt | ลบไฟล์ชื่อ "example.txt" |
cp example.txt MyFolder\example.txt | คัดลอกไฟล์ "example.txt" ไปยังโฟลเดอร์ "MyFolder" |
mv MyFolder\example.txt AnotherFolder\example.txt | ย้ายไฟล์ "example.txt" จากโฟลเดอร์ "MyFolder" ไปยังโฟลเดอร์ "AnotherFolder" |
rni example.txt new_example.txt | เปลี่ยนชื่อไฟล์ "example.txt" เป็น "new_example.txt" |
Modules
เราสามารถติดตั้ง Modules ต่างๆได้ ค้นหา Module ที่
Powershell Gallery Packages
ดู Module ที่ติดตั้ง => พิมพ์ Get-InstalledModule
Script
เราสามารถเขียน Script ต่างๆได้ =>
รายละเอียดเพิ่มเติม
powershell
# ตัวอย่าง scripe
function Say-Hello {
param($name)
Write-Output "สวัสดี, $name!"
}
Say-Hello "คุณผู้ใช้"
Customization
Starship
Starship => ทำ prompt shell ให้สวยๆ
- ติดตั้ง Nerd Font
⚠️ ติดตั้ง Nerd font ด้วย เพื่อใช้ symbol ไม่เพี้ยน
ติดตั้ง
Nerd Font ให้เรียบร้อย
font ที่ติดตั้ง
- กำหนดใน Windows Terminal
- ติดตั้ง starship
scoop install starship
winget install --id Starship.Starship
- ทำให้ powershell เรียกใช้ starship อัตโนมัติ
powershell
# เรียกใช้ starship ทุกครั้งเมื่อเปิด PowerShell
Invoke-Expression (&starship init powershell)
- ใช้งานได้แล้ว