Bỏ qua

Các lệnh PowerShell thường dùng

PowerShell là một ngôn ngữ shell. Shell là cái vỏ bảo vệ nhân của hệ điều hành

Lịch sử

Lệnh Cách dùng
Cuộn lên, cuộn xuống Alt+↑, Alt+↓
Xem lịch sử các lệnh get-history hoặc h
Tìm một lệnh mình từng dùng Gõ lệnh đó rồi nhấn F8
Nguồn:: [about History - PowerShell Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_history?view=powershell-7.3)

The Complete Guide to PowerShell Punctuation - Simple Talk

Lệnh Cách dùng
Markmode Ctrl+Shift+M
Tạo pane mới Alt+Shift+-, Alt+Shift++
Thay đổi kích cỡ pane Alt+Shift+↑, Alt+Shift+↓

Tạo nhiều thư mục

$list=(ls -name -directory).substring(1)
foreach ($i in $list) {
    $index=$i.substring(0,1)
    cd "2$i" 
    new-item "2$index`1 Thành quả cần có" -type directory;
    new-item "2$index`2 Sự kiện" -type directory;
    new-item "2$index`3 Tài liệu" -type directory;
    Cd ..
}

Tạo array

$list|ForEach-Object {"`"$_`"," } |clip

Đổi tên hàng loạt

Get-ChildItem *.md, *.json -recurse | Where-Object {$_.name -cmatch '^2[A-Z]'}  | Rename-Item -newname { $_.name -replace '^2(.*)', '4$1'} -whatif 
  • -cmatch: match có case sensitive

Tìm các đường dẫn tới các tệp chứa một chuỗi nhất định

Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path

Tìm và thay chuỗi hàng loạt

VS Code nhiều khi không tìm hết file được do tên quá dài

Get-ChildItem *.md, *.json -recurse | ForEach-Object { (Get-Content $_).Replace('Kết quả cần có::','Thành quả cần có::').Replace('kết-quả-cần-có','thành-quả-cần-có') | Set-Content $_ } 

Thêm nội dung vào hàng loạt tập tin

Get-ChildItem .gitignore -recurse | ForEach-Object { 
    Add-Content $_ .obsidian/plugins/obsidian-mkdocs-publisher/logs.txt
}

Xoá tất cả desktop.ini

Get-ChildItem -Force -Recurse -File -Filter "desktop.ini" | Remove-Item -force

Tắt giới hạn số ký tự tối đa cho đường dẫn

New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force

Maximum Path Length Limitation - Win32 apps | Microsoft Learn

Thêm biến môi trường

[System.Environment]::SetEnvironmentVariable('ResourceGroup','AZ_Resource_Group', 'User')
$env:PATH += ";SomeRandomPath"

[Environment]::SetEnvironmentVariable("Path",    [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";C:\bin", [EnvironmentVariableTarget]::Machine)

sfd

$sourcePath = F:\New folder  
$destinationPath = E:\New folder  
$files = Get-ChildItem -Path $sourcePath -Recurse -Filter *.*  
foreach($file in $files){  
    $sourcePathFile = $file.FullName  
    $destinationPathFile = $file.FullName.Replace($sourcePath, $destinationPath)  
    $exists = Test-Path $destinationPathFile  
    if(!$exists){  
    $dir = Split-Path -parent $destinationPathFile  
    if (!(Test-Path($dir))) { New-Item -ItemType directory -Path $dir }  
    Copy-Item -Path $sourcePathFile -Destination $destinationPathFile -Recurse -Force  
    }  
    else{  
        $isFile = Test-Path -Path $destinationPathFile -PathType Leaf  
    if(!$isFile){  
        Copy-Item -Path $sourcePathFile -Destination $destinationPathFile -Recurse -Force  
    }  
    }  
}