Bỏ qua

PowerShell là một ngôn ngữ shell
The Complete Guide to PowerShell Punctuation - Simple Talk

Tạo nhiều folder

$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 -recurse | Where-Object {$_.name -cmatch '^2[A-Z]'}  | Rename-Item -newname { $_.name -replace '^2(.*)', '4$1'} -whatif 
  • -cmatch: match có case sensitive

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

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

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

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  
    }  
    }  
}

Cập nhật lần cuối : 27 tháng 11, 2023
Tạo : 1 tháng 5, 2023