Hỏi đáp PowerShell bằng tiếng Việt

By | April 12, 2015

translated-vietnamese


Khái niệm: Các câu hỏi thường gặp về Powershell.

Bạn có thể sử dụng danh sách này theo những cách khác nhau:

  • Để sao chép / dán các lệnh thành một kịch bản
  • Để xem một cách nhanh chóng các cú pháp của một lệnh cụ thể
  • Để nâng cao kiến thức kỹ thuật của bạn
  • Để khám phá các lệnh mới
  • Để chuẩn bị cho một cuộc phỏng vấn việc làm

Cập nhật
16 Tháng Bảy 2015
Tác giả powershell-guru.com
Nguồn vietnamese.powershell-guru.com
Danh mục
75
Các câu hỏi
610


ACL
Active Directory
Alias
Arrays
Browsers
Certificates
Characters
CIM
Comments
COM Objects
Compare
Computer
Credentials
CSV
Culture
Date
Drives
Environment
Errors
Event Viewer
Files
Folders
Format Operator (-f)
Functions
GPO
GUI
Hardware
Hashtables
Help
History
Jobs
Keyboard
Loops
Math
Memory
Messages
Modules
Microsoft Excel
Microsoft Exchange
Microsoft Outlook
Microsoft SharePoint
Networking
Openfiles
Operators
Parameters
Password
Powershell ISE
Powershell v5
Printers
Processes
PSObject
Quest
Random
RDP
Regedit
Regex
Remote
Restore
Scheduled Tasks
Search
SCCM
Services
SMTP
Snapins
Sounds
Static .NET Methods
Strings
System
Try/Catch
Variables
Symantec Vault
Windows 2012
Windows Azure
Windows Forms
WMI
XML

System

Làm thế nào để xác định phiên bản PowerShell của tôi?

Làm thế nào để chạy Powershell trong một phiên bản tương thích ngược?
powershell.exe -Version 2.0

Làm thế nào để yêu cầu phiên bản Powershell tối thiểu (3,0 và cao hơn) trong một kịch bản với PowerShell?
#Requires -Version 3.0

Làm thế nào để yêu cầu quyền quản trị cho một kịch bản với Powershell?

Làm thế nào để kiểm tra các thông số của một kịch bản với Powershell?
help -Name .\Get-ExchangeEnvironmentReport.ps1 -Full

Làm thế nào để lấy thông tin người sử dụng hiện tại với Powershell?
[Security.Principal.WindowsIdentity]::GetCurrent()

Làm thế nào để tạo, chỉnh sửa và tải lại một hồ sơ cá nhân với Powershell?

Làm thế nào để ltạm ngừng 5 giây / phút trong một kịch bản với Powershell?
Start-Sleep -Seconds 5
Start-Sleep -Seconds 300 # 5 minutes

Làm thế nào để lấy thời gian khởi động cuối cùng với Powershell?
(Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime

Làm thế nào để có được loại máy gia tốc với Powershell?

Làm thế nào để liệt kê các chương trình khởi động với Powershell?

Làm thế nào để gỡ bỏ cài đặt một ứng dụng với Powershell?

Làm thế nào để chụp ảnh màn hình desktop hoặc một cửa sổ đang hoạt động với Powershell?
Take-ScreenShot -Screen -File 'C:\scripts\screenshot.png' -Imagetype JPEG
Repository : Take-ScreenShot

Làm thế nào để có được số lượng tin nhắn cho hàng đợi MSMQ với Powershell?

Làm thế nào để thiết lập các chính sách thực thi với Powershell?

Làm thế nào để tạo ra một shortcut với Powershell?

Làm thế nào để gắn hoặc gỡ bỏ một chương trình khỏi thanh tác vụ với Powershell?

Làm thế nào để mở một cửa sổ Explorer với Powershell?
[Diagnostics.Process]::Start('explorer.exe')
Invoke-Item -Path C:\Windows\explorer.exe

Làm thế nào để liệt kê các trình điều khiển thiết bị với Powershell?
Get-WmiObject -Class Win32_PnPSignedDriver
Get-WindowsDriver -Online -All
driverquery.exe

Làm thế nào để tạo ra một GUID với Powershell?

Làm thế nào để có được vị trí của thư mục tạm thời cho người sử dụng hiện tại với Powershell?
[System.IO.Path]::GetTempPath()

Làm thế nào để nhập một đường dẫn và đường dẫn vào một đường dẫn duy nhất với PowerShell?
Join-Path -Path C:\ -ChildPath \windows

Làm thế nào để liệt kê tất cả các lệnh “Get-*” với Powershell?
Get-Command -Verb Get

Làm thế nào để liệt kê các thư mục hệ thống đặc biệt với Powershell?

Làm thế nào để mount tập tin định dạng ISO / VHD với Powershell?
Mount-DiskImage 'D:\ISO\file.iso' # ISO
Mount-DiskImage 'D:\VHD\file.vhd' # VHD

Làm thế nào để kiểm tra phiên bản .NET Framework đã được cài đặt với Powershell?

Làm thế nào để kiểm tra xem nếu phiên bản .NET Framework 4.5 được cài đặt với Powershell?
(Get-ItemProperty -Path 'HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full' -EA 0).Version -like '4.5*'

Làm thế nào để bắt đầu và dừng lại một bảng điểm (để tạo ra một kỷ lục của phiên Windows PowerShell) với Powershell?
Start-Transcript -Path 'C:\scripts\transcript.txt
Stop-Transcript

Làm thế nào để thay đổi thư mục hiện tại tới một địa điểm cụ thể với Powershell?
Set-Location -Path 'C:\scripts'

Làm thế nào để xóa màn hình với Powershell?
Clear-Host
cls # Alias

Làm thế nào để thay đổi độ phân giải màn hình hiển thị với Powershell?
Set-DisplayResolution -Width 1280 -Height 1024 -Force # Windows 2012

Làm thế nào để thiết lập toàn màn hình với Powershell?
mode.com 300

Làm thế nào để có được kích thước (chiều rộng và chiều cao) của một bức tranh với Powershell?

Làm thế nào để có được khóa sản phẩm Windows với Powershell?

Perfmon

Làm thế nào để có được hiện tại “% Processor Time” (trung bình) trong 5 giây cuối cùng (10 lần) với Powershell?
(Get-Counter '\Processor(_total)\% Processor Time' -SampleInterval 5 -MaxSamples 10).CounterSamples.CookedValue

Assemblies

Làm thế nào để nạp những thành phần với Powershell?

Làm thế nào để kiểm tra các phần của .NET hiện đang nạp với Powershell?

Làm thế nào để tìm ra đường dẫn tới GAC ​​(Global Assembly Cache) với Powershell?

Clipboard

Làm thế nào để sao chép kết quả vào clipboard với Powershell?

Làm thế nào để có được nội dung của clipboard với Powershell?
Add-Type -AssemblyName PresentationCore
[Windows.Clipboard]::GetText()

Hotfixes

Làm thế nào để có được các hotfix đã được cài đặt với Powershell?
Get-HotFix -ComputerName $computer

Làm thế nào để có được các hotfix đã được cài đặt trước / sau một ngày cụ thể với Powershell?
Get-HotFix | Where-Object -FilterScript { $_.InstalledOn -lt ([DateTime]'01/01/2015') } # Before 01/01/2015
Get-HotFix | Where-Object -FilterScript {$_.InstalledOn -gt ([DateTime]'01/01/2015')} # After 01/01/2015

Làm thế nào để kiểm tra xem một hotfix được cài đặt với Powershell?
Get-HotFix -Id KB2965142

Làm thế nào để lấy được các hotfix được cài đặt trên một máy tính từ xa với Powershell?
Get-HotFix -ComputerName $computer

Pagefile

Làm thế nào để nhận được thông tin Pagefile với Powershell?
Get-WmiObject -Class Win32_PageFileusage | Select-Object -Property Name, CurrentUsage, AllocatedBaseSize, PeakUsage, InstallDate

Làm thế nào để có được kích thước đề nghị (MB) cho Pagefile với Powershell?
[Math]::Truncate(((Get-WmiObject -Class Win32_ComputerSystem).TotalPhysicalMemory) / 1MB) * 1.5

Làm thế nào để tạo ra một Pagefile (4096 MB) trên ổ đĩa D với Powershell?

Làm thế nào để xóa một Pagefile trên ổ C: với Powershell?

Maintenance

Làm thế nào để kiểm tra các phân mảnh của một ổ đĩa bằng Powershell?

Làm thế nào để kiểm tra không gian đĩa của ổ đĩa với Powershell?

Up


Files

Làm thế nào để mở một tập tin với Powershell?
Invoke-Item -Path 'C:\scripts\file.txt'
.'C:\scripts\file.txt'

Làm thế nào để đọc một tập tin với Powershell?
Get-Content -Path 'C:\scripts\file.txt'
gc "C:\scripts\file.txt" # Alias

Làm thế nào để xuất ra một file với Powershell?

Làm thế nào để có được tên đầy đủ của tập tin kịch bản hiện tại với Powershell?
$MyInvocation.MyCommand.Path

Làm thế nào để nén (compress / zip) tập tin với Powershell?

Làm thế nào để giải nén (uncompress / unzip) các tập tin với Powershell?

Làm thế nào để xem các tập tin trong một tập tin ZIP với Powershell?
Add-Type -AssemblyName 'System.IO.Compression.Filesystem'
[System.IO.Compression.ZipFile]::OpenRead($fileZIP)

Làm thế nào để hiển thị kích thước của một tập tin trong KB với Powershell?
(Get-ChildItem -Path .\winsrv.dll).Length /1KB
(Get-ChildItem -Path .\winsrv.dll).Length /1MB
(Get-ChildItem -Path .\winsrv.dll).Length /1GB

Làm thế nào để tìm các tập tin lớn hơn hoặc ít hơn 1 GB với Powershell?

Làm thế nào để hiển thị tên của một tập tin mà không có phần mở rộng với Powershell?
[System.IO.Path]::GetFileNameWithoutExtension('C:\Windows\system32\calc.exe') # Return calc

Làm thế nào để hiển thị phần mở rộng của một tập tin với Powershell?
[System.IO.Path]::GetExtension('C:\scripts\file.txt') # Return .txt

Làm thế nào để có được các phiên bản của một tập tin với Powershell?

Làm thế nào để có được hash của một tập tin Powershell?
(Get-FileHash $file).Hash

Làm thế nào để có được các checksum MD5 / SHA1 của một tập tin với Powershell?
Get-FileHash $file -Algorithm MD5
Get-FileHash $file -Algorithm SHA1

Làm thế nào để hiển thị các tập tin ẩn với Powershell?

Làm thế nào để kiểm tra xem nếu một tập tin có phần mở rộng với Powershell?

Làm thế nào để thiết lập một tập tin là Read Only với Powershell?
Set-ItemProperty -Path .\file.txt -Name IsReadOnly -Value $true

Làm thế nào để thay đổi các thuộc tính LastWriteTime với tuần trước cho một tập tin với Powershell?
Set-ItemProperty -Path .\file.txt -Name LastWriteTime -Value ((Get-Date).AddDays(-7))
If not working, use Nirsoft tool: BulkFileChanger.

Làm thế nào để tạo ra một tập tin mới với Powershell?
New-Item -ItemType File -Path 'C:\scripts\file.txt' -Value 'FirstLine'

Làm thế nào để đổi tên một tập tin với Powershell?
Rename-Item -Path 'C:\scripts\file.txt' -NewName 'C:\scripts\powershellguru2.txt'

Làm thế nào để đổi tên hàng loạt nhiều tập tin với Powershell?
Get-ChildItem -Path C:\scripts\txt | Rename-Item -NewName { $_.Name -replace ' ', '_' }

Làm thế nào để xóa một tập tin với Powershell?
Remove-Item -Path 'C:\scripts\file.txt'

Làm thế nào để hiển thị 10 dòng mới nhất của một tập tin với Powershell?
Get-Content -Path 'C:\scripts\log.txt' -Tail 10

Làm thế nào để mở khóa một số tập tin của một thư mục với Powershell?
Get-ChildItem -Path 'C:\scripts\Modules' | Unblock-File

Làm thế nào để loại bỏ các dòng trống từ một tập tin với Powershell?
(Get-Content -Path file.txt) | Where-Object -FilterScript {$_.Trim() -ne '' } | Set-Content -Path file.txt

Làm thế nào để kiểm tra xem một tập tin có tồn tại với Powershell?

Làm thế nào để lấy ra tập tin mới nhất/cũ nhất được tạo ra trong một thư mục với Powershell?

Làm thế nào để loại bỏ các dòng trùng lặp từ một tập tin với Powershell?

Làm thế nào để có được các tập tin được tạo ra nhiều hơn / ít hơn 1 tháng trong một thư mục với Powershell?

Làm thế nào để có được các tập tin được tạo ra nhiều hơn / ít hơn 1 năm trong một thư mục với Powershell?

Làm thế nào để xuất ra giá trị của một biến cho một tập tin với Powershell?
Set-Content -Path file.txt -Value $variable

Làm thế nào để đếm số lượng các tập tin (* .txt) trong một thư mục với Powershell?

Làm thế nào để tìm kiếm một chuỗi trong nhiều tập tin với PowerShell?
Select-String -Path 'C:\*.txt' -Pattern 'Test'

Làm thế nào để hiển thị dòng đầu tiên / dòng cuối cùng của một tập tin với PowerShell?

Làm thế nào để hiển thị một số dòng cụ thể của một tập tin với PowerShell?

Làm thế nào để đếm số dòng của một tập tin với PowerShell?

Làm thế nào để đếm số ký tự và lời nói của một tập tin với PowerShell?

Làm thế nào để tải về một tập tin với Powershell?
Invoke-WebRequest -Uri 'http://www.nirsoft.net/utils/searchmyfiles.zip' -OutFile 'C:\tools\searchmyfiles.zip'

Làm thế nào để hiển thị đường dẫn đầy đủ của tập tin với Powershell?
Resolve-Path -Path .\script.ps1 # Return C:\Scripts\script.ps1

Copy

Làm thế nào để sao chép một tập tin vào một thư mục với Powershell?
Copy-Item -Path 'C:\source\file.txt' -Destination 'C:\destination'

Làm thế nào để sao chép một tập tin vào nhiều thư mục trong PowerShell?

Làm thế nào để sao chép nhiều tập tin vào một thư mục trong PowerShell?
Get-ChildItem -Path 'C:\source' -Filter *.txt | Copy-Item -Destination 'C:\destination'

Up


Active Directory

Domain & Forest

Computers

Groups

Organizational Unit (OU)

Users

Domain & Forest

Làm thế nào để tìm thấy máy chủ Global Catalog trong Active Directory với Powershell?
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().GlobalCatalogs

Làm thế nào để tìm các site trong Active Directory với Powershell?
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites

Làm thế nào để tìm ra máy chủ hiện tại điều khiển miền với Powershell?

Làm thế nào để tìm thấy tất cả các máy chủ điều khiển miền trong một miền với Powershell?

Làm thế nào để tìm thấy những thất bại nhân rộng AD với Powershell?
Get-ADReplicationFailure dc02.domain.com # Windows 8 and 2012

Làm thế nào để tìm ra thời gian tồn tại của tombstone cho forest trong Active Directory với Powershell?

Làm thế nào để có được thông tin chi tiết của một forest / miền trong Active Directory với Powershell?

Làm thế nào để lấy đường dẫn của “Deleted Objects” container trong Active Directory với Powershell?
(Get-ADDomain).DeletedObjectsContainer

Làm thế nào để kích hoạt tính năng AD Recycle Bin trong Active Directory với Powershell?

Làm thế nào để khôi phục lại một tài khoản AD từ Recycle Bin trong Active Directory với Powershell?
Get-ADObject -Filter 'samaccountname -eq "powershellguru"' -IncludeDeletedObjects | Restore-ADObject

Làm thế nào để tìm ra vai trò FSMO với Powershell?

Làm thế nào để kết nối với một bộ điều khiển tên miền cụ thể với Powershell?
Get-ADUser -Identity $user -Server 'serverDC01'

Làm thế nào để biết được máy chủ đăng nhập hiện tại với Powershell?

Làm thế nào để thực hiện “gpupdate” trên một máy tính với Powershell?
Invoke-GPUpdate -Computer $computer -Force -RandomDelayInMinutes 0 # Windows 2012

Groups

Làm thế nào để tạo ra một nhóm mới trong Active Directory với Powershell?

Làm thế nào để xóa một nhóm trong Active Directory với Powershell?
Remove-ADGroup -Identity 'PowershellGuru'

Làm thế nào để thêm một người dùng vào nhóm trong Active Directory với Powershell?
Add-ADGroupMember "Powershell Guru" -Members powershellguru

Làm thế nào để loại bỏ một người dùng từ một nhóm trong Active Directory với Powershell?
Remove-ADGroupMember 'Powershell Guru' -Members powershellguru

Làm thế nào để tìm thấy các nhóm rỗng (không có thành viên) trong Active Directory với Powershell?
Get-ADGroup -Filter * -Properties Members | Where-Object -FilterScript {-not $_.Members}

Làm thế nào để đếm các nhóm rỗng (không có thành viên) trong Active Directory với Powershell?
(Get-ADGroup -Filter * -Properties Members | Where-Object -FilterScript {-not $_.Members}).Count

Làm thế nào để có được các thành viên của một nhóm trong Active Directory với Powershell?

Làm thế nào để có được các thành viên của một nhóm với các thành viên đệ quy trong Active Directory với Powershell?

Làm thế nào để đếm số lượng thành viên của một nhóm có / không có thành viên đệ quy trong Active Directory với Powershell?

Users

Làm thế nào để sử dụng một wildcard trong các bộ lọc của “Get-ADUser” trong Active Directory với Powershell?

Làm thế nào để di chuyển người dùng đến một OU khác trong Active Directory với Powershell?
Move-ADObject -Identity $dn -TargetPath 'OU=myOU,DC=domain,DC=com'

Làm thế nào để tìm thấy tất cả các thành viên đã được (Nested) cho một người dùng với Powershell?
Get-ADGroup -LDAPFilter "(member:1.2.840.113556.1.4.1941:=$($dn))"

Làm thế nào để có được các thành viên (tên ngắn / cắt ngắn) cho một người dùng với Powershell?
(Get-ADUser $user -Properties MemberOf).MemberOf | ForEach-Object -Process {($_ -split ',')[0].Substring(3)} | Sort-Object

Làm thế nào để thay đổi Name (Họ và tên), DisplayName, givenName (FirstName) Họ và tên (LastName) cho một tài khoản người dùng trong Active Directory với Powershell?

Làm thế nào để thay đổi các mô tả, văn phòng và (Tele) số điện thoại cho một tài khoản người dùng trong Active Directory với Powershell?
Set-ADUser $samAccountName -Description 'IT Consultant' -Office 'Building B' -OfficePhone '12345'

Làm thế nào để thiết lập ngày hết hạn đến 31/12/2015″ hoặc “Không bao giờ” cho một tài khoản người dùng trong Active Directory với Powershell?

Làm thế nào để mở khóa một tài khoản người dùng trong Active Directory với Powershell?
Unlock-ADAccount $samAccountName

Làm thế nào để kích hoạt / vô hiệu hóa tài khoản người dùng trong Active Directory với Powershell?

Làm thế nào để loại bỏ một tài khoản người dùng trong Active Directory với Powershell?
Remove-ADUser $samAccountName

Làm thế nào để thiết lập lại mật khẩu cho tài khoản của một người dùng trong Active Directory với Powershell?

Làm thế nào để thiết lập lại mật khẩu cho nhiều tài khoản người dùng (số lượng lớn) trong Active Directory với Powershell?

Làm thế nào để tìm chủ sở hữu của một tập tin trong Active Directory với Powershell?

Làm thế nào để tìm các OU (Organizational Unit) cho một người dùng trong Active Directory với Powershell?
[regex]::match("$((Get-ADUser $user -Properties DistinguishedName).DistinguishedName)",'(?=OU=)(.*\n?)').value

Làm thế nào để tìm thấy các tài khoản người dùng bị vô hiệu hóa trong Active Directory với Powershell?

Làm thế nào để tìm thấy các tài khoản người dùng đã hết hạn trong Active Directory với Powershell?
Search-ADAccount -AccountExpired

Làm thế nào để tìm thấy các tài khoản người dùng bị khóa trong Active Directory với Powershell?
Search-ADAccount -LockedOut

Làm thế nào để tìm các SID của tài khoản người dùng trong Active Directory với Powershell?
(Get-ADUser $user -Properties SID).SID.Value

Làm thế nào để chuyển đổi tên người dùng thành SID trong Active Directory với Powershell?

Làm thế nào để chuyển đổi một SID thành tên người dùng trong Active Directory với Powershell?

Làm thế nào để phân chia các Tên phân biệt của một tài khoản người dùng với Active Directory với Powershell?

Làm thế nào để tìm thấy những ngày tạo / sửa đổi của một tài khoản người dùng với Active Directory với Powershell?
Get-ADUser -Identity $user -Properties whenChanged, whenCreated | Format-List -Property whenChanged, whenCreated

Làm thế nào để hiển thị các thuộc tính tùy chọn và bắt buộc đối với các lớp “User” với Active Directory với Powershell?

Làm thế nào để có được đường dẫn LDAP cho một người sử dụng với Active Directory với Powershell?

Làm thế nào để thay đổi các CN (Canonical Name) cho một người sử dụng với Active Directory với Powershell?
Rename-ADObject $((Get-ADUser $user -Properties DistinguishedName).DistinguishedName) -NewName 'Test Powershell'

Làm thế nào để có được các Organizational Unit (OU) Parent của một người sử dụng trong Active Directory với Powershell?

Làm thế nào để có được các chủ sở hữu của một tài khoản người dùng(người đã tạo ra các tài khoản) với Active Directory với Powershell?

Làm thế nào để chuyển đổi các thuộc tính pwdLastSet cho một người sử dụng với Active Directory với Powershell?

Computers

Làm thế nào để kiểm tra các kênh an toàn giữa các máy tính cục bộ và miền với Powershell?
Test-ComputerSecureChannel

Làm thế nào để sửa chữa các kênh an toàn giữa các máy tính cục bộ và miền với Powershell?
Test-ComputerSecureChannel -Repair

Làm thế nào để vô hiệu hóa tài khoản máy tính trong Active Directory với Powershell?
Disable-ADAccount $computer

Làm thế nào để tìm các máy tính với hệ điều hành cụ thể trong Active Directory với Powershell?

Organizational Unit (OU)

Làm thế nào để tạo ra một Organizational Unit (OU) trong Active Directory với Powershell?
New-ADOrganizationalUnit -Name 'TEST' -Path 'DC=domain,DC=com'

Làm thế nào để có được chi tiết về Organizational Unit (OU) trong Active Directory với Powershell?
Get-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' -Properties *

Làm thế nào để thay đổi mô tả của một Organizational Unit (OU) trong Active Directory với Powershell?
Set-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' -Description 'My description'

Làm thế nào để kích hoạt / vô hiệu hóa một đơn vị tổ chức (OU) từ việc vô tình xóa trong Active Directory với Powershell?

Làm thế nào để kích hoạt tình cờ xóa cho tất cả các Organizational Unit (OU) trong Active Directory với Powershell?

Làm thế nào để xóa một Organizational Unit (OU) đã được bảo vệ khỏi việc vô tình xóa trong Active Directory với Powershell?

Làm thế nào để chuyển đổi một Organizational Unit của DistinguishedName (OU) thành CanonicalName trong Active Directory với Powershell?

Làm thế nào để liệt kê các Organizational Unit (OU) rỗng với Powershell?

Làm thế nào để có được người quản lý của một nhóm với Powershell?
(Get-ADGroup $dn -Properties Managedby).Managedby

Up


Regex (Regular Expression)

Làm thế nào để trích xuất một địa chỉ IP v4 (80.80.228.8) với Regex với Powershell?
$example = 'The IP address is 80.80.228.8'
$ip = [regex]::match($example,'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b').value

Làm thế nào để trích xuất một địa chỉ MAC (C0-D9-62-39-61-2D) với dấu phân cách “-” với Regex với Powershell?
$example = 'The MAC address is C0-D9-62-39-61-2D'
$mac = [regex]::match($example,'([0-9A-F]{2}[-]){5}([0-9A-F]{2})').value

Làm thế nào để trích xuất một địa chỉ MAC (C0: D9: 62: 39: 61: 2D) với dấu phân cách “:” với Regex với Powershell?
$example = 'The MAC address is C0:D9:62:39:61:2D'
$mac = [regex]::match($example,'((\d|([a-f]|[A-F])){2}:){5}(\d|([a-f]|[A-F])){2}').value

Làm thế nào để trích xuất một ngày (2015/10/02) với Regex với Powershell?
$example = 'The date is 10/02/2015'
$date = [regex]::match($example,'(\d{2}\/\d{2}\/\d{4})').value

Làm thế nào để trích xuất một URL (www.powershell-guru.com) với Regex với Powershell?
$example = 'The URL is www.powershell-guru.com'
$url = [regex]::match($example,'[a-z]+[:.].*?(?=\s)').value

Làm thế nào để trích xuất một email (user@domain.com) với Regex với Powershell?
$example = 'The email is user@domain.com'
$email = [regex]::match($example,'(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b').value

Làm thế nào để trích xuất “guru” từ chuỗi ví dụ với Regex với Powershell?
$example = 'www.powershell-guru.com'
[regex]::match($example,'(?<=-)(.*\n?)(?=.com)').value

Làm thế nào để trích xuất “guru.com” từ chuỗi ví dụ với Regex với Powershell?
$example = 'www.powershell-guru.com'
[regex]::match($example,'(?<=-)(.*\n?)(?<=.)').value

Làm thế nào để trích xuất “powershell-guru.com” từ chuỗi ví dụ với Regex với Powershell?
$example = 'www.powershell-guru.com'
[regex]::match($example,'(?<=www.)(.*\n?)').value

Làm thế nào để trích xuất “123” từ chuỗi ví dụ với Regex với Powershell?
$example = 'Powershell123'
[regex]::match($example,'(\d+)').value

Làm thế nào để trích xuất “$” (ký hiệu đô la) từ chuỗi ví dụ với Regex với Powershell?
$example = 'Powershell`$123'
[regex]::match($example,'(\$)').value

Làm thế nào để thay thế một ký tự (* .com) với nhau (* .fr) trong một chuỗi với Regex với Powershell?
$example = 'www.powershell-guru.com'
[regex]::Replace($example, '.com','.fr')

Làm thế nào để thoát khỏi một chuỗi với Regex với Powershell?
[regex]::Escape('\\server\share')

Up


Memory

Làm thế nào để lấy một tập hợp của bộ nhớ bằng “garbage collector “với Powershell?
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

Làm thế nào để có được kích thước bộ nhớ RAM của máy tính với Powershell?

Up


Date

Làm thế nào để có được ngày hiện tại với Powershell?
Get-Date
[Datetime]::Now

Làm thế nào để hiển thị ngày trong các định dạng khác nhau với Powershell?

Làm thế nào để chuyển đổi một ngày (định dạng ngày giờ) sang định dạng (String) với Powershell?

Làm thế nào để chuyển đổi một ngày (định dạng String) sang một ngày (định dạng ngày giờ)với Powershell?

Làm thế nào để tính toán sự khác biệt (số ngày, giờ, phút hoặc giây) giữa hai ngày với Powershell?
(New-TimeSpan -Start $dateStart -End $dateEnd).Days
(New-TimeSpan -Start $dateStart -End $dateEnd).Hours
(New-TimeSpan -Start $dateStart -End $dateEnd).Minutes
(New-TimeSpan -Start $dateStart -End $dateEnd).Seconds

Làm thế nào để so sánh hai ngày với Powershell?
(Get-Date 2015-01-01) -lt (Get-Date 2015-01-30) # True
(Get-Date 2015-01-01) -gt (Get-Date 2015-01-30) # False

Làm thế nào để sắp xếp một hàng của ngày với định dạng kiểu ngày giờ với Powershell?
$arrayDate | Sort-Object -Property {$_ -as [Datetime]}

Làm thế nào để bắt đầu và dừng lại một đồng hồ bấm giờ với Powershell?
$chrono = [Diagnostics.Stopwatch]::StartNew()
$chrono.Stop()
$chrono

Làm thế nào để có được ngày hiện tại trong tuần với Powershell?
(Get-Date).DayOfWeek #Sunday

Làm thế nào để có được ngày hôm qua với Powershell?
(Get-Date).AddDays(-1)

Làm thế nào để có được số ngày trong một tháng (vào tháng Hai năm 2015) với Powershell?
[DateTime]::DaysInMonth(2015, 2)

Làm thế nào để biết nếu năm nhuận với Powershell?
[DateTime]::IsLeapYear(2015)

Làm thế nào để liệt kê các múi giờ với Powershell?
[System.TimeZoneInfo]::GetSystemTimeZones()

Up


Networking

Làm thế nào để mã hóa (định dạng ASCII) và giải mã một URL với Powershell?

Tương đương với các lệnh mạng bản địa với Powershell là gì?

Làm thế nào để có được địa chỉ IP với Powershell?
Get-NetIPAddress # Windows 8.1 & Windows 2012
Get-NetIPConfiguration # Windows 8.1 & Windows 2012

Làm thế nào để vô hiệu hóa địa chỉ IP v6 (IPv6) với Powershell?

Làm thế nào để xác nhận một địa chỉ IP v4 (IPv4) với Powershell?
if([ipaddress]'10.0.0.1'){'validated'}

Làm thế nào để tìm địa chỉ IP bên ngoài với Powershell?

Làm thế nào để tìm ra tên máy từ một địa chỉ IP với Powershell?
([System.Net.Dns]::GetHostEntry($IP)).Hostname

Làm thế nào để tìm địa chỉ IP từ một Hostname với Powershell?
([System.Net.Dns]::GetHostAddresses($computer)).IPAddressToString

Làm thế nào để tìm các FQDN từ một Hostname với Powershell?
[System.Net.Dns]::GetHostByName($computer).HostName

Làm thế nào để tìm thấy những cấu hình mạng (IP, Subnet, Gateway và DNS) với Powershell?

Làm thế nào để tìm địa chỉ MAC với Powershell?
Get-CimInstance win32_networkadapterconfiguration | Select-Object -Property Description, Macaddress
Get-WmiObject -Class win32_networkadapterconfiguration | Select-Object -Property Description, Macaddress

Làm thế nào để ping một máy tính với Powershell?

Làm thế nào để kiểm tra xem một máy tính được kết nối với Internet với Powershell?

Làm thế nào để thực hiện một tra cứu whois cho một trang web với PowerShell?
$whois = New-WebServiceProxy 'http://www.webservicex.net/whois.asmx?WSDL'
$whois.GetWhoIs('powershell-guru.com')

Làm thế nào để có được thông tin chi tiết của IP công cộng (Geolocation) với Powershell?

Làm thế nào để kiểm tra xem một port được mở / đóng với Powershell?
New-Object -TypeName Net.Sockets.TcpClient -ArgumentList $computer, 135

Làm thế nào để thực hiện tracert với Powershell?
Test-NetConnection www.google.com -TraceRoute

Làm thế nào để sửa chữa một home network đã kết nối với Powershell?
Get-NetAdapter | Format-Table -Property Name, InterfaceDescription, ifIndex -AutoSize # Windows 8.1
Set-NetConnectionProfile -InterfaceIndex 6 -NetworkCategory Private

Làm thế nào để hiển thị các kết nối cổng TCP với Powershell?
netstat.exe -ano
Get-NetTCPConnection #Windows 8 and 2012

Làm thế nào để rút ngắn một URL dài thành một URL nhỏ với Powershell?
$url = 'www.powershell-guru.com'
$tiny = Invoke-RestMethod -Uri "http://tinyurl.com/api-create.php?url=$url"

Làm thế nào để có được các thiết lập proxy với Powershell?
Get-ItemProperty -Path HKCU:"Software\Microsoft\Windows\CurrentVersion\Internet Settings"

DNS

Làm thế nào để kiểm tra bộ nhớ cache DNS trên máy tính cục bộ với Powershell?
ipconfig.exe /displaydns
Get-DnsClientCache #Windows 8 and 2012

Làm thế nào để xóa bộ nhớ cache DNS trên máy tính cục bộ với Powershell?
ipconfig.exe /flushdns
Start-Process -FilePath ipconfig -ArgumentList /flushdns -WindowStyle Hidden
Clear-DnsClientCache #Windows 8 and 2012

Làm thế nào để xóa bộ nhớ cache DNS trên máy tính từ xa với Powershell?
Invoke-Command -ScriptBlock {Clear-DnsClientCache} -ComputerName computer01, computer02

Làm thế nào để đọc file Hosts với Powershell?
Get-Content -Path 'C:\Windows\system32\drivers\etc\hosts'

Up


Password

Làm thế nào để tạo ra một mật khẩu ngẫu nhiên với Powershell?
[Reflection.Assembly]::LoadWithPartialName('System.Web')
[System.Web.Security.Membership]::GeneratePassword(30,2)

Làm thế nào để thay đổi mật khẩu cục bộ cho một quản trị viên trên máy chủ từ xa với Powershell?
$admin = [ADSI]('WinNT://server01/administrator,user')
$admin.SetPassword($password)
$admin.SetInfo()

Làm thế nào để tìm ra ngày hết hạn mật khẩu của một tài khoản trong Active Directory với Powershell?

Up


Printers

Làm thế nào để liệt kê tất cả các máy in cho một máy chủ cụ thể với Powershell?
Get-WmiObject -Query 'Select * From Win32_Printer' -ComputerName $computer

Làm thế nào để liệt kê tất cả các cổng cho một máy chủ cụ thể với Powershell?
Get-WmiObject -Class Win32_TCPIPPrinterPort -Namespace 'root\CIMV2' -ComputerName $computer

Làm thế nào để thay đổi những bình luận / vị trí cho một máy in với Powershell?

Làm thế nào để tẩy (hủy bỏ tất cả các công việc) cho một máy in với Powershell?
$printer = Get-WmiObject -Class win32_printer -Filter "Name='HP Deskjet 2540 series'"
$printer.CancelAllJobs()

Làm thế nào để in một trang thử nghiệm cho một máy in với Powershell?
$printer = Get-WmiObject -Class win32_printer -Filter "Name='HP Deskjet 2540 series'"
$printer.PrintTestPage()

Làm thế nào để có được hàng đợi in cho các máy in với Powershell?

Up


Regedit

Read

Làm thế nào để liệt kê các registry hives với Powershell?
Get-ChildItem -Path Registry::

Làm thế nào để có được các giá trị registry và loại giá trị với Powershell?

Làm thế nào để liệt kê các khóa con registry key với Powershell?

Làm thế nào để liệt kê các khóa con registry key theo cách đệ quy với Powershell?
Get-ChildItem -Path 'HKLM:\SYSTEM' -Recurse -ErrorAction SilentlyContinue

Làm thế nào để tìm các khóa con với một tên cụ thể với Powershell?
Get-ChildItem -Path 'HKLM:\SOFTWARE' -Include *Plugin* -Recurse -ErrorAction SilentlyContinue

Làm thế nào để trả về chỉ có tên của những khóa con của registry với Powershell?
(Get-ChildItem -Path 'HKLM:\SYSTEM').Name # Return HKEY_LOCAL_MACHINE\SYSTEM\ControlSet
Get-ChildItem -Path 'HKLM:\SYSTEM' -Name # Return ControlSet

Làm thế nào để liệt kê các giá trị registry với Powershell?
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'

Làm thế nào để đọc một giá trị registry cụ thể vớiPowershell?
(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').ProductName

Làm thế nào để đọc một giá trị registry cụ thể trên máy tính từ xa với Powershell?

Write

Làm thế nào để tạo ra một khóa registry mới với Powershell?
New-Item -Path 'HKCU:\Software\MyApplication'

Làm thế nào để tạo ra một giá trị registry với Powershell?
New-ItemProperty -Path 'HKCU:\Software\MyApplication' -Name 'Version' -Value '1.0'

Làm thế nào để sửa đổi một giá trị registry đã tồn tại với Powershell?
Set-ItemProperty -Path 'HKCU:\Software\MyApplication' -Name 'Version' -Value '2.0'

Delete

Làm thế nào để xóa một giá trị registry với Powershell?
Remove-ItemProperty -Path 'HKCU:\Software\MyApplication' -Name 'Version'

Làm thế nào để xóa một khóa registry với Powershell?
Remove-Item -Path 'HKCU:\Software\MyApplication' -Force

Test

Làm thế nào để kiểm tra nếu một khóa registry tồn tại với Powershell?
Test-Path -Path 'HKCU:\Software\MyApplication'

Làm thế nào để kiểm tra xem một giá trị registry tồn tại với Powershell?
(Get-Item -Path 'HKCU:\Software\MyApplication').GetValueNames() -contains 'Version'

Up


Strings

Làm thế nào để loại bỏ các ký tự khoảng trắng từ khởi đầu của một chuỗi với Powershell?
$string = ' PowershellGuru'
$string = $string.TrimStart()

Làm thế nào để loại bỏ các ký tự khoảng trắng từ cuối của một chuỗi với Powershell?
$string = 'PowershellGuru '
$string = $string.TrimEnd()

Làm thế nào để loại bỏ các ký tự khoảng trắng (bắt đầu và kết thúc) của một chuỗi với Powershell?
$string = ' PowershellGuru '
$string = $string.Trim()

Làm thế nào để chuyển đổi một chuỗi sang chữ hoa với Powershell?
$string = 'powershellguru'
$string = $string.ToUpper()

Làm thế nào để chuyển đổi một chuỗi sang chữ thường với Powershell?
$string = 'POWERSHELLGURU'
$string = $string.ToLower()

Làm thế nào để chọn ra chuỗi “Powershell” của chuỗi “PowershellGuru” với Powershell?
$string.Substring(0,10)

Làm thế nào để chọn ra chuỗi con “Guru” của chuỗi “PowershellGuru” với Powershell?
$string.Substring(10)

Làm thế nào để chọn số “123” của “Powershell123Guru” với Powershell?
$string = 'Powershell123Guru'
[regex]::match($string,'(\d+)').value

Làm thế nào để có được các chỉ số không dựa trên các “Guru” của chuỗi “PowershellGuru” với Powershell?
$string.IndexOf('Guru') # 10

Làm thế nào để kiểm tra xem một chuỗi là null hoặc trống rỗng, với Powershell?
$string = $null
$string = ''
[string]::IsNullOrEmpty($string)

Làm thế nào để kiểm tra xem một chuỗi là null, trống rỗng, hoặc chỉ bao gồm các ký tự khoảng trắng với Powershell?
$string = $null
$string = ''
$string = ' '
[string]::IsNullOrWhiteSpace($string)

Làm thế nào để kiểm tra xem một chuỗi có chứa một kỳ tự cụ thể với Powershell?
$string = 'PowershellGuru'
$string.Contains('s')
[regex]::match($string,'s').Success

Làm thế nào để trả về độ dài của một chuỗi với Powershell?
$string.Length

Làm thế nào để nối hai chuỗi với Powershell?

Làm thế nào để kết hợp một hoặc nhiều dấu ngoặc “[]” trong một chuỗi với Powershell?
$string = '[PowershellGuru]'
$string -match '\[' # Only 1
$string -match '\[(.*)\]' # Several

Làm thế nào để kết hợp một hoặc nhiều dấu ngoặc đơn “()” trong một chuỗi với Powershell?
$string = '(PowershellGuru)'
$string -match '\(' # Only 1
$string -match '\((.*)\)' # Several

Làm thế nào để kết hợp cho một hoặc một số trong ngoặc “{}” trong một chuỗi với Powershell?
$string = '{PowershellGuru}'
$string -match '\{' # Only 1
$string -match '\{(.*)\}' # Several

Làm thế nào để kết hợp một hoặc nhiều dấu ngoặc nhọn “& lt; & gt;” trong một chuỗi với Powershell?
$string = ''
$string -match '\<' # Only 1
$string -match "\<(.*)\>" # Several

Làm thế nào để kết hợp bất kỳ chữ thường (abc) trong một chuỗi với Powershell?
$string = 'POWERSHELLGURU'
$string -cmatch "^[a-z]*$" #False

Làm thế nào để kết hợp bất kỳ upperletters (ABC) trong một chuỗi với Powershell?
$string = 'powershellguru'
$string -cmatch "^[A-Z]*$" #False

Làm thế nào để kết hợp “[p” (p chữ thường) trong một chuỗi với Powershell?
$string = '[powershellGuru]'
$string -cmatch '\[[a-z]\w+' #True

Làm thế nào để kết hợp “[P” (P trường hợp trên) trong một chuỗi với Powershell?
$string = '[PowershellGuru]'
$string -cmatch '\[[A-Z]\w+' #True

Làm thế nào để thay thế một dòng bằng một dòng khác với Powershell?
$a = 'Line A'
$b = 'Line B'
$a = $a -replace $a, $b

Làm thế nào để chuyển đổi một hoạt động phân chia thành một chuỗi (Tỷ lệ phần trăm) với Powershell?
(1/2).ToString('P')

Làm thế nào để sắp xếp chuỗi chứa số với Powershell?

Làm thế nào để chọn từ cuối cùng của một câu với Powershell?
$sentence = 'My name is Test Powershell'
$sentence.Split(' ')[-1] # Returns Powershell

Làm thế nào để có được chữ lớn nhất của một câu với Powershell?
$sentence = 'My name is Test Powershell'
$sentence.Split(' ') | Sort-Object -Property Length | Select-Object -Last 1 # Returns Powershell

Làm thế nào để đếm số lần một chuỗi xuất hiện trong một câu với Powershell?
$sentence = 'test test test Powershell'
[regex]::Matches($sentence, 'test').Count # Returns 3

Làm thế nào để sao chép mỗi ký tự trong chuỗi thành một mảng ký tự với Powershell?

Làm thế nào để chuyển đổi các chữ cái đầu tiên của một chuỗi thành chữ hoa với Powershell?

Làm thế nào để thêm vào (trái hoặc phải) một chuỗi với Powershell?

Làm thế nào để mã hóa và giải mã một chuỗi Base64 với Powershell?

Làm thế nào để chuyển đổi một thành nhị phân và ngược lại với Powershell?

Làm thế nào để chỉ trở về thư mục mẹ gần nhất trong một đường dẫn với Powershell?

Làm thế nào để trở về chỉ mục gần nhất trong một đường dẫn với Powershell?

Up


Math

Làm thế nào để liệt kê các phương thức của lớp System.Math với Powershell?
[System.Math] | Get-Member -Static -MemberType Method

Làm thế nào để trả lại giá trị tuyệt đối với Powershell?
[Math]::Abs(-12) #Returns 12
[Math]::Abs(-12.5) # Returns 12.5

Làm thế nào để trả lại các góc mà sin là một số xác định với Powershell?
[Math]::ASin(1) #Returns 1,5707963267949

Làm thế nào để trả lại giá trị cao nhất với Powershell?
[Math]::Ceiling(1.4) #Returns 2
[Math]::Ceiling(1.9) #Returns 2

Làm thế nào để trở lại sàn với Powershell?
[Math]::Floor(1.4) #Returns 1
[Math]::Floor(1.9) #Returns 1

Làm thế nào để trả lại đối số tự nhiên (bese e) của một số xác định với Powershell?
[Math]::Log(4) #Returns 1,38629436111989

Làm thế nào để trả lại logarit cơ số 10 của một số quy định với Powershell?
[Math]::Log10(4) #Returns 0,602059991327962

Làm thế nào để trả lại tối đa của hai giá trị với Powershell?
[Math]::Max(2,4) #Returns 4
[Math]::Max(-2,-4) #Returns -2

Làm thế nào để trả lại tối thiểu của hai giá trị với Powershell?
[Math]::Min(2,4) #Returns 2
[Math]::Max(-2,-4) #Returns -4

Làm thế nào để trả lại một số lũy thừa chỉ định với Powershell?
[Math]::Pow(2,4) #Returns 16

Làm thế nào để trả lại một giá trị thập phân với giá trị tích phân gần nhất Powershell?
[Math]::Round(3.111,2) #Returns 3,11
[Math]::Round(3.999,2) #Returns 4

Làm thế nào để trả lại giá trị tích phân của một số thập phân chỉ định với Powershell?
[Math]::Truncate(3.111) #Returns 3
[Math]::Truncate(3.999) #Returns 3

Làm thế nào để trở về căn bậc hai của một số quy định với Powershell?
[Math]::Sqrt(16) #Returns 4

Làm thế nào để trả lại số Pi với Powershell?
[Math]::Pi #Returns 3,14159265358979

Làm thế nào để trả lại các logarit tự nhiên (không đổi e) với Powershell?
[Math]::E #Returns 2,71828182845905

Làm thế nào để kiểm tra xem một số là số chẵn hoặc lẻ với Powershell?
[bool]($number%2)

Up


Hashtables

Làm thế nào để tạo ra một hashtable trống với Powershell?
$hashtable = @{}
$hashtable = New-Object -TypeName System.Collections.Hashtable

Làm thế nào để tạo ra một hashtable với các kí tự với Powershell?

Làm thế nào để tạo ra một hashtable được sắp xếp theo key / Tên (theo thứ tự từ điển ) với các kí tự với Powershell?

Làm thế nào để thêm các mục (cặp key-value) với một bảng băm với Powershell?
$hashtable.Add('Key4', 'Value4')

Làm thế nào để có được một giá trị cụ thể của một Hashtable với Powershell?

Làm thế nào để có được giá trị tối thiểu của một Hashtable với Powershell?

Làm thế nào để có được giá trị tối đa của một Hashtable với Powershell?

Làm thế nào để sửa đổi các mục trong một Hashtable với Powershell?
$hashtable.Set_Item('Key1', 'Value1Updated')

Làm thế nào để loại bỏ các mục trong một Hashtable với Powershell?
$hashtable.Remove('Key1')

Làm thế nào để xóa một hashtable với Powershell?
$hashtable.Clear()

Làm thế nào để kiểm tra sự hiện diện của một chìa khóa / giá trị cụ thể trong một Hashtable với Powershell?
$hashtable.ContainsKey('Key3')
$hashtable.ContainsValue('Value3')

Làm thế nào để sắp xếp theo chìa khóa / giá trị trong một Hashtable với Powershell?
$hashtable.GetEnumerator() | Sort-Object -Property Name
$hashtable.GetEnumerator() | Sort-Object -Property Value -Descending

Up


Arrays

Làm thế nào để tạo ra một mảng trống rỗng, với Powershell?
$array = @()
$array = [System.Collections.ArrayList]@()

Làm thế nào để tạo ra một mảng với các vật phẩm với Powershell?
$array = @('A', 'B', 'C')
$array = 'A', 'B', 'C'
$array = 'a,b,c'.Split(',')
$array = .{$args} a b c
$array = echo a b c

Làm thế nào để thêm các mục vào một mảng với Powershell?
$array += 'D'
[void]$array.Add('D')

Làm thế nào để chỉnh sửa một mục trong một mảng với Powershell?
$array[0] = 'Z' # 1st item[0]

Làm thế nào để kiểm tra kích thước của một mảng với Powershell?
$array = 'A', 'B', 'C'
$array.Length # Returns 3

Làm thế nào để lấy một mục / một số / tất cả các mục trong một mảng với Powershell?
$array = @('A', 'B', 'C')
$array[0] # One item (A)
$array[0] + $array[2] # Several items (A,C)
$array # All items (A,B,C)

Làm thế nào để loại bỏ các mục trống trong một mảng với Powershell?
$array = @('A', 'B', 'C', '')
$array = $array.Split('',[System.StringSplitOptions]::RemoveEmptyEntries) | Sort-Object # A,B,C

Làm thế nào để kiểm tra xem một mục tồn tại trong một mảng với Powershell?
$array = @('A', 'B', 'C')
'A' | ForEach-Object -Process {$array.Contains($_)} # Returns True
'D' | ForEach-Object -Process {$array.Contains($_)} # Returns False

Làm thế nào để tìm số chỉ số của một mục trong một mảng với Powershell?
$array = @('A', 'B', 'C')
[array]::IndexOf($array,'A') # Returns 0

Làm thế nào để đảo ngược thứ tự của các mục trong một mảng với Powershell?
$array = @('A', 'B', 'C')
[array]::Reverse($array) # C,B,A

Làm thế nào để tạo ra một