<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PowerShell &#8211; Nhật Phúc</title>
	<atom:link href="https://nhatphuc.com/bai-viet/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://nhatphuc.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Sep 2024 02:26:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://nhatphuc.com/wp-content/uploads/2019/08/favicon-75x75.png</url>
	<title>PowerShell &#8211; Nhật Phúc</title>
	<link>https://nhatphuc.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Cách hiển thị Unicode trong PowerShell</title>
		<link>https://nhatphuc.com/cach-hien-thi-unicode-trong-powershell/</link>
					<comments>https://nhatphuc.com/cach-hien-thi-unicode-trong-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Thu, 26 Sep 2024 02:26:45 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Windows Terminal]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12398</guid>

					<description><![CDATA[<p>Trong quá trình làm việc với PowerShell, đôi khi chúng ta cần hiển thị đường [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-hien-thi-unicode-trong-powershell/">Cách hiển thị Unicode trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Trong quá trình làm việc với PowerShell, đôi khi chúng ta cần hiển thị đường dẫn đầy đủ của các tệp chứa các ký tự từ các ngôn ngữ khác nhau như tiếng Ả Rập, tiếng Trung, tiếng Nhật và tiếng Nga. Tuy nhiên, khi thực thi lệnh, kết quả trả về thường là các ký tự không rõ ràng, thay thế bởi các dấu &#8220;?&#8221; thay vì các ký tự chính xác.</p>



<p>Ví dụ lệnh được thực hiện là:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">(Get-ChildItem -Recurse -Path "D:\test" -Include *unicode* | Get-ChildItem -Recurse).FullName</pre>



<p>Kết quả đầu ra là các ký tự không thể đọc được như hình minh họa.</p>



<p></p>



<h2 class="wp-block-heading">Vấn đề với Windows Console</h2>



<p>Đây là vấn đề thường gặp với hệ thống Windows Console Subsystem, vốn không hỗ trợ đầy đủ Unicode. Khi sử dụng các ngôn ngữ có ký tự đặc biệt như tiếng Ả Rập, tiếng Trung, hoặc tiếng Nhật, việc hiển thị chính xác trở nên khó khăn hơn.</p>



<h2 class="wp-block-heading">Giải pháp: Sử dụng Windows Terminal</h2>



<p>Một trong những giải pháp tốt nhất hiện nay là sử dụng <strong>Windows Terminal</strong>, được phân phối và cập nhật qua Microsoft Store trên Windows 10 và Windows 11. Terminal này hỗ trợ Unicode tốt hơn nhiều so với console truyền thống (conhost.exe).</p>



<h4 class="wp-block-heading">Các bước thực hiện:</h4>



<ol class="wp-block-list">
<li><strong>Chọn font hỗ trợ Unicode</strong>: Đầu tiên, hãy chọn một font TrueType hỗ trợ đầy đủ các ngôn ngữ cần thiết. Ví dụ, để hỗ trợ các ký tự tiếng Ả Rập, Trung Quốc, Nhật Bản và Nga, bạn có thể sử dụng font <strong>SimSun-ExtB</strong> có sẵn trên Windows 10.</li>



<li><strong>Chuyển mã hóa console sang UTF-8</strong>: Để làm cho PowerShell nhận dạng và xử lý Unicode đúng cách, bạn cần chuyển mã hóa console sang UTF-8. Bạn có thể thực hiện điều này bằng cách sử dụng lệnh:<code><strong>$OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding </strong></code><br>Lệnh này đảm bảo rằng PowerShell sử dụng mã hóa UTF-8 để giao tiếp với các chương trình bên ngoài.</li>



<li><strong>Lưu cấu hình mã hóa</strong>: Để duy trì cấu hình này cho các phiên làm việc PowerShell sau, bạn có thể thêm lệnh trên vào tệp cấu hình <code>$PROFILE</code> của PowerShell. Điều này giúp bạn không phải cấu hình lại mỗi khi mở PowerShell.</li>



<li><strong>Sử dụng Windows Terminal</strong>: Nếu bạn đang sử dụng Windows 11, phiên bản 22H2, <strong>Windows Terminal</strong> đã trở thành console mặc định. Nó cung cấp hỗ trợ tốt hơn cho các ký tự Unicode, giúp hiển thị chính xác các ký tự đặc biệt từ nhiều ngôn ngữ khác nhau.</li>
</ol>



<h3 class="wp-block-heading">Một số vấn đề khác cần lưu ý:</h3>



<ul class="wp-block-list">
<li>Khi giao tiếp với các chương trình bên ngoài, đặc biệt là những chương trình yêu cầu mã hóa UTF-8, việc sử dụng cấu hình mã hóa đúng là rất quan trọng. Các công cụ như <strong>Node.js</strong> hay <strong>Python</strong> thường hỗ trợ UTF-8 tốt nếu được cấu hình đúng cách.</li>



<li>Một số chương trình cũ có thể không hỗ trợ mã hóa UTF-8, dẫn đến việc hiển thị không chính xác. Trong trường hợp này, bạn có thể tạm thời chuyển mã hóa console sang UTF-8 khi gọi chương trình đó, rồi sau đó khôi phục lại cấu hình cũ.</li>
</ul>



<p>Bằng cách thực hiện các bước trên, bạn có thể đảm bảo rằng các ký tự Unicode sẽ được hiển thị đúng trong PowerShell, giúp việc làm việc với các tệp tin có tên chứa các ký tự đặc biệt trở nên dễ dàng hơn.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-hien-thi-unicode-trong-powershell/">Cách hiển thị Unicode trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/cach-hien-thi-unicode-trong-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cách kiểm tra phiên bản PowerShell đã cài đặt</title>
		<link>https://nhatphuc.com/cach-kiem-tra-phien-ban-powershell-da-cai-dat/</link>
					<comments>https://nhatphuc.com/cach-kiem-tra-phien-ban-powershell-da-cai-dat/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Thu, 26 Sep 2024 02:21:42 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[Command Prompt]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12394</guid>

					<description><![CDATA[<p>PowerShell là một công cụ dòng lệnh mạnh mẽ được phát triển bởi Microsoft, giúp [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-kiem-tra-phien-ban-powershell-da-cai-dat/">Cách kiểm tra phiên bản PowerShell đã cài đặt</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>PowerShell là một công cụ dòng lệnh mạnh mẽ được phát triển bởi Microsoft, giúp quản lý hệ thống và tự động hóa các tác vụ. Để biết chính xác phiên bản PowerShell đang được cài đặt trên hệ thống của bạn, bạn có thể sử dụng một số lệnh đơn giản. Hướng dẫn này giúp bạn biết cách kiểm tra phiên bản PowerShell trên máy tính.</p>



<h2 class="wp-block-heading">Kiểm tra phiên bản PowerShell từ PowerShell</h2>



<p>Bạn có thể sử dụng lệnh sau để kiểm tra phiên bản PowerShell đang chạy:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$PSVersionTable.PSVersion</pre>



<p>Lệnh này sẽ hiển thị thông tin về phiên bản của PowerShell, bao gồm:</p>



<ul class="wp-block-list">
<li><strong>Major</strong>: Phiên bản chính.</li>



<li><strong>Minor</strong>: Phiên bản phụ.</li>



<li><strong>Build</strong>: Số build.</li>



<li><strong>Revision</strong>: Số sửa đổi.</li>
</ul>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="436" height="145" src="https://nhatphuc.com/wp-content/uploads/2024/09/powershell-version.png" alt="Cách kiểm tra phiên bản PowerShell đã cài đặt 1" class="wp-image-12395" title="Cách kiểm tra phiên bản PowerShell đã cài đặt 1" srcset="https://nhatphuc.com/wp-content/uploads/2024/09/powershell-version.png 436w, https://nhatphuc.com/wp-content/uploads/2024/09/powershell-version-320x106.png 320w, https://nhatphuc.com/wp-content/uploads/2024/09/powershell-version-265x88.png 265w, https://nhatphuc.com/wp-content/uploads/2024/09/powershell-version-364x121.png 364w, https://nhatphuc.com/wp-content/uploads/2024/09/powershell-version-144x48.png 144w, https://nhatphuc.com/wp-content/uploads/2024/09/powershell-version-289x96.png 289w" sizes="(max-width: 436px) 100vw, 436px" /></figure>



<p><code><br></code></p>



<h2 class="wp-block-heading">Kiểm tra phiên bản PowerShell từ Command Prompt</h2>



<p>Nếu bạn muốn kiểm tra phiên bản PowerShell từ Command Prompt mà không cần mở PowerShell, bạn có thể chạy lệnh sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">powershell -Command "$PSVersionTable.PSVersion"</pre>



<p>Lệnh này sẽ mở PowerShell, thực thi câu lệnh <code>$PSVersionTable.PSVersion</code> và trả về kết quả trực tiếp trên cửa sổ Command Prompt.</p>



<h2 class="wp-block-heading">Khi PowerShell không được cài đặt</h2>



<p>Nếu lệnh trên không hoạt động hoặc không có biến <code>$PSVersionTable</code>, điều này có nghĩa là phiên bản PowerShell của bạn có thể là phiên bản 1.0, phiên bản cũ nhất không hỗ trợ biến này. Khi đó, bạn nên kiểm tra xem PowerShell có được cài đặt hay không.</p>



<p>Bạn có thể kiểm tra qua Registry bằng cách vào đường dẫn sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\Install</pre>



<p>Nếu giá trị là <code>1</code>, PowerShell đã được cài đặt.</p>



<p>Việc biết rõ phiên bản PowerShell sẽ giúp bạn biết được các tính năng mà phiên bản của bạn hỗ trợ, đồng thời giúp bạn cập nhật lên phiên bản mới nhất khi cần thiết.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-kiem-tra-phien-ban-powershell-da-cai-dat/">Cách kiểm tra phiên bản PowerShell đã cài đặt</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/cach-kiem-tra-phien-ban-powershell-da-cai-dat/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cách thiết lập environment variables trong PowerShell</title>
		<link>https://nhatphuc.com/cach-thiet-lap-environment-variables-trong-powershell/</link>
					<comments>https://nhatphuc.com/cach-thiet-lap-environment-variables-trong-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Thu, 26 Sep 2024 02:18:04 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12390</guid>

					<description><![CDATA[<p>Khi làm việc với PowerShell, nhiều người nhận thấy rằng việc thiết lập biến môi [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-thiet-lap-environment-variables-trong-powershell/">Cách thiết lập environment variables trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Khi làm việc với PowerShell, nhiều người nhận thấy rằng việc thiết lập biến môi trường như <code>PATH</code> chỉ có tác dụng trong phiên hiện tại và không tồn tại sau khi đóng PowerShell. Bài viết này giúp bạn tìm hiểu cách thay đổi biến môi trường trong PowerShell và cách lưu các thay đổi này vĩnh viễn.</p>



<h2 class="wp-block-heading">1. Thay đổi biến môi trường trong phiên PowerShell</h2>



<p>Trong PowerShell, biến môi trường có thể được thay đổi tạm thời bằng cách sử dụng cú pháp sau:</p>



<ul class="wp-block-list">
<li>Hiển thị biến môi trường <code>PATH</code> hiện tại: <code><strong>$env:Path</strong></code></li>



<li>Thêm một đường dẫn mới vào <code>PATH</code> ở đầu danh sách: <code><strong>$env:Path = "C:\new_path;" + $env:Path</strong></code></li>



<li>Thêm một đường dẫn mới vào cuối <code>PATH</code>: <code><strong>$env:Path += ";C:\new_path"</strong></code></li>
</ul>



<p>Tuy nhiên, các thay đổi này chỉ tồn tại trong phiên hiện tại của PowerShell. Sau khi đóng và mở lại PowerShell, biến môi trường sẽ quay trở về trạng thái ban đầu.</p>



<h2 class="wp-block-heading">2. Lưu thay đổi vĩnh viễn</h2>



<p>Để làm cho thay đổi biến môi trường trở nên vĩnh viễn, bạn có thể sử dụng một trong hai cách sau:</p>



<h3 class="wp-block-heading">Sử dụng PowerShell Profiles</h3>



<p>PowerShell cung cấp tệp cấu hình (profile) để tự động thực hiện các lệnh mỗi khi khởi chạy PowerShell. Bạn có thể thêm các thiết lập biến môi trường vào tệp profile để đảm bảo chúng luôn được thiết lập mỗi khi bạn mở PowerShell.</p>



<ul class="wp-block-list">
<li>Kiểm tra vị trí của tệp profile: <code><strong>$profile</strong></code></li>



<li>Mở tệp profile để chỉnh sửa: <code><strong>notepad $profile</strong></code></li>
</ul>



<p>Trong tệp này, bạn có thể thêm dòng lệnh để thiết lập biến môi trường:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$env:Path += ";C:\new_path"</pre>



<p>Lưu tệp và mỗi lần mở PowerShell, đường dẫn mới sẽ được tự động thêm vào biến môi trường.</p>



<h3 class="wp-block-heading">Sử dụng <code>Set-EnvironmentVariable</code></h3>



<p>Bạn cũng có thể sử dụng phương pháp thay đổi biến môi trường trực tiếp từ registry bằng cách dùng <code><strong>Set-EnvironmentVariable</strong></code>. Cú pháp cho việc này như sau:</p>



<ul class="wp-block-list">
<li>Thay đổi biến môi trường hệ thống: <code><strong>[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\new_path", [System.EnvironmentVariableTarget]::Machine)</strong></code></li>



<li>Thay đổi biến môi trường người dùng: <code><strong>[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\new_path", [System.EnvironmentVariableTarget]::User)</strong></code></li>
</ul>



<p>Lưu ý rằng thay đổi này sẽ có hiệu lực sau khi bạn khởi động lại PowerShell hoặc mở một phiên mới.</p>



<h2 class="wp-block-heading">3. Lưu ý khi thao tác với <code>PATH</code></h2>



<p>Khi thực hiện thay đổi <code>PATH</code>, hãy cẩn thận để không làm mất các đường dẫn quan trọng khác. Một cách an toàn là sao lưu <code>PATH</code> trước khi thay đổi bằng lệnh:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$env:Path >> path_backup.txt</pre>



<h2 class="wp-block-heading">4. Xóa đường dẫn khỏi <code>PATH</code></h2>



<p>Để xóa một đường dẫn khỏi <code>PATH</code>, bạn có thể sử dụng phương pháp sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$env:Path = ($env:Path -split ";") | Where-Object { $_ -ne "C:\old_path" } -join ";"</pre>



<p>Bạn có thể sử dụng PowerShell để quản lý biến môi trường là một cách mạnh mẽ và linh hoạt. Với các bước trên, bạn sẽ có thể kiểm soát biến môi trường <code>PATH</code> và các biến khác một cách hiệu quả.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-thiet-lap-environment-variables-trong-powershell/">Cách thiết lập environment variables trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/cach-thiet-lap-environment-variables-trong-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cách ghi file trong PowerShell với mã hoá UTF-8 without the BOM</title>
		<link>https://nhatphuc.com/cach-ghi-file-trong-powershell-voi-ma-hoa-utf-8-without-the-bom/</link>
					<comments>https://nhatphuc.com/cach-ghi-file-trong-powershell-voi-ma-hoa-utf-8-without-the-bom/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Thu, 26 Sep 2024 02:06:37 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12387</guid>

					<description><![CDATA[<p>Trong PowerShell, khi bạn muốn ghi dữ liệu vào file với mã hóa UTF-8 mà [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-ghi-file-trong-powershell-voi-ma-hoa-utf-8-without-the-bom/">Cách ghi file trong PowerShell với mã hoá UTF-8 without the BOM</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Trong PowerShell, khi bạn muốn ghi dữ liệu vào file với mã hóa UTF-8 mà không có BOM (Byte Order Mark), điều này không thể thực hiện được một cách trực tiếp bằng các lệnh thông thường như <code>Out-File</code> hoặc <code>Set-Content</code>, vì mặc định các lệnh này sẽ thêm BOM vào file. Tuy nhiên, bạn có thể sử dụng phương pháp khác để đảm bảo file được ghi với mã hóa UTF-8 without the BOM.</p>



<h2 class="wp-block-heading">BOM là gì?</h2>



<p>BOM (Byte Order Mark) là một chuỗi byte ở đầu file để chỉ định loại mã hóa của file. Trong trường hợp của UTF-8, BOM thường là ba byte: 0xEF, 0xBB, 0xBF. Một số hệ thống hoặc phần mềm không nhận dạng chính xác file có BOM, dẫn đến việc dữ liệu bị lỗi hoặc không thể đọc được.</p>



<h3 class="wp-block-heading">Vấn đề khi ghi file với BOM trong PowerShell</h3>



<p>Khi sử dụng PowerShell để ghi file, các lệnh như <code>Out-File</code> hoặc <code>Set-Content</code> sẽ mặc định thêm BOM vào file nếu bạn sử dụng mã hóa UTF-8, điều này có thể gây ra vấn đề khi làm việc với một số ứng dụng không hỗ trợ BOM.</p>



<h2 class="wp-block-heading">Cách ghi file với mã hóa UTF-8 không có BOM</h2>



<p>Để ghi file trong PowerShell với mã hóa UTF-8 không có BOM, bạn có thể sử dụng lớp <code>.NET</code> <code>System.IO.File</code> và tạo một đối tượng <code>UTF8Encoding</code> với tham số <code>False</code> để chỉ định không sử dụng BOM.</p>



<h4 class="wp-block-heading">Ví dụ mã PowerShell:</h4>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Nội dung file cần ghi
$NoiDungFile = Get-Content "C:\DuongDanCuaFile.txt"

# Tạo đối tượng mã hóa UTF-8 không có BOM
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False

# Ghi nội dung vào file với mã hóa UTF-8 không có BOM
[System.IO.File]::WriteAllLines("C:\DuongDanCuaFileMoi.txt", $NoiDungFile, $Utf8NoBomEncoding)</pre>



<h3 class="wp-block-heading">Giải thích đoạn mã:</h3>



<ul class="wp-block-list">
<li><code>$NoiDungFile</code>: Biến này chứa nội dung của file hiện tại mà bạn muốn ghi vào file mới.</li>



<li><code>$Utf8NoBomEncoding</code>: Đây là đối tượng mã hóa UTF-8 không có BOM. Khi tham số <code>False</code> được truyền vào, nó đảm bảo rằng BOM sẽ không được thêm vào file khi ghi.</li>



<li><code>WriteAllLines</code>: Phương thức này từ lớp <code>.NET</code> sẽ ghi nội dung vào file với mã hóa UTF-8 không có BOM. Bạn chỉ cần chỉ định đường dẫn của file đích, nội dung cần ghi và đối tượng mã hóa.</li>
</ul>



<h2 class="wp-block-heading">Lợi ích của mã hóa UTF-8 không BOM</h2>



<ul class="wp-block-list">
<li>Giúp các hệ thống hoặc phần mềm không hỗ trợ BOM có thể đọc file một cách chính xác.</li>



<li>Giữ nguyên định dạng của file mà không thêm bất kỳ ký tự đặc biệt nào vào đầu file.</li>



<li>Đảm bảo tính tương thích cao hơn với các công cụ và ứng dụng khác nhau, đặc biệt là trong môi trường lập trình và quản lý dữ liệu.</li>



<li></li>
</ul>



<p>Việc ghi file với mã hóa UTF-8 không có BOM trong PowerShell không còn là vấn đề phức tạp nếu bạn biết cách sử dụng các lớp <code>.NET</code> để xử lý. Phương pháp này đặc biệt hữu ích trong việc đảm bảo tính tương thích và tránh các lỗi tiềm ẩn khi làm việc với các file dữ liệu đa nền tảng.</p>



<p>Với hướng dẫn này, bạn có thể dễ dàng tùy chỉnh và áp dụng cho các kịch bản khác nhau trong công việc hằng ngày khi sử dụng PowerShell để quản lý và xử lý file.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-ghi-file-trong-powershell-voi-ma-hoa-utf-8-without-the-bom/">Cách ghi file trong PowerShell với mã hoá UTF-8 without the BOM</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/cach-ghi-file-trong-powershell-voi-ma-hoa-utf-8-without-the-bom/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>4 cách chạy lệnh PowerShell dưới quyền Admin</title>
		<link>https://nhatphuc.com/chay-lenh-powershell-duoi-quyen-admin/</link>
					<comments>https://nhatphuc.com/chay-lenh-powershell-duoi-quyen-admin/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Thu, 26 Sep 2024 02:02:32 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12384</guid>

					<description><![CDATA[<p>Khi bạn muốn chạy một tập lệnh PowerShell dưới quyền quản trị viên, bạn có [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/chay-lenh-powershell-duoi-quyen-admin/">4 cách chạy lệnh PowerShell dưới quyền Admin</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Khi bạn muốn chạy một tập lệnh PowerShell dưới quyền quản trị viên, bạn có thể làm theo các bước sau để thực hiện điều này mà không cần phải nhập mật khẩu mỗi lần:</p>



<h2 class="wp-block-heading">1. <strong>Sử dụng lệnh <code>Start-Process</code> với tùy chọn <code>runAs</code></strong>:</h2>



<ol class="wp-block-list"></ol>



<p>Lệnh này khởi động một phiên PowerShell mới dưới quyền quản trị viên:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Start-Process powershell -Verb runAs</pre>



<p>Lệnh trên sẽ mở một cửa sổ PowerShell mới dưới quyền quản trị viên.</p>



<h2 class="wp-block-heading">2. <strong>Kiểm tra nếu người dùng hiện tại đã có quyền quản trị</strong>:</h2>



<ol start="2" class="wp-block-list"></ol>



<p>Nếu người dùng hiện tại không có quyền quản trị, tập lệnh sẽ khởi động lại PowerShell với quyền quản trị viên. Đây là một ví dụ đầy đủ:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {  
  $arguments = "&amp;amp; '" +$myinvocation.mycommand.definition + "'"
  Start-Process powershell -Verb runAs -ArgumentList $arguments
  Break
}</pre>



<h2 class="wp-block-heading">3. <strong>Chạy script với quyền quản trị viên</strong>:</h2>



<ol start="3" class="wp-block-list"></ol>



<p>Nếu bạn đang viết một script PowerShell và muốn đảm bảo rằng nó luôn chạy dưới quyền quản trị viên, bạn có thể sử dụng đoạn mã sau để kiểm tra và yêu cầu quyền quản trị viên nếu cần thiết:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit
}

# Mã của bạn sẽ nằm dưới đây</pre>



<h2 class="wp-block-heading"><strong>Tạo shortcut để chạy script với quyền quản trị</strong>:</h2>



<ol start="4" class="wp-block-list"></ol>



<ul class="wp-block-list">
<li>Tạo một file shortcut trỏ đến tập lệnh PowerShell của bạn.</li>



<li>Nhấp chuột phải vào shortcut, chọn <strong>Properties</strong>.</li>



<li>Ở tab <strong>Shortcut</strong>, nhấp <strong>Advanced</strong> và chọn <strong>Run as administrator</strong>.</li>
</ul>



<p>Điều này sẽ cho phép bạn luôn chạy script với quyền quản trị viên mà không cần xác nhận lại mỗi lần.</p>



<p>Các bước này giúp bạn dễ dàng chạy lệnh PowerShell với quyền quản trị mà không cần xác nhận lại mỗi lần.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/chay-lenh-powershell-duoi-quyen-admin/">4 cách chạy lệnh PowerShell dưới quyền Admin</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/chay-lenh-powershell-duoi-quyen-admin/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cách thay đổi mã hóa đầu ra mặc định thành UTF-8 trong PowerShell</title>
		<link>https://nhatphuc.com/cach-thay-doi-ma-hoa-dau-ra-mac-dinh-thanh-utf-8-trong-powershell/</link>
					<comments>https://nhatphuc.com/cach-thay-doi-ma-hoa-dau-ra-mac-dinh-thanh-utf-8-trong-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Wed, 25 Sep 2024 08:42:50 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12379</guid>

					<description><![CDATA[<p>Trong PowerShell, đầu ra mặc định khi ghi vào tệp thông qua các lệnh hoặc [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-thay-doi-ma-hoa-dau-ra-mac-dinh-thanh-utf-8-trong-powershell/">Cách thay đổi mã hóa đầu ra mặc định thành UTF-8 trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Trong PowerShell, đầu ra mặc định khi ghi vào tệp thông qua các lệnh hoặc lệnh chuyển hướng như <code>&gt;</code> hoặc <code>Out-File</code> thường sử dụng mã hóa UTF-16, điều này có thể gây khó khăn nếu bạn muốn sử dụng UTF-8. Dưới đây là cách để thay đổi mã hóa mặc định sang UTF-8.</p>



<h2 class="wp-block-heading">1. Sử dụng $PSDefaultParameterValues</h2>



<p>Để đặt mã hóa mặc định cho các lệnh như <code>Out-File</code>, bạn có thể sử dụng biến <code>$PSDefaultParameterValues</code>. Điều này sẽ giúp bạn không cần chỉ định mã hóa mỗi khi sử dụng <code>Out-File</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'</pre>



<p>Nếu bạn muốn áp dụng điều này cho tất cả các cmdlet hỗ trợ tham số <code>-Encoding</code> như <code>Set-Content</code> hoặc <code>Add-Content</code>, bạn có thể thiết lập mã hóa mặc định cho tất cả các lệnh:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$PSDefaultParameterValues['*:Encoding'] = 'utf8'</pre>



<p><strong>Lưu ý:</strong></p>



<ul class="wp-block-list">
<li>Trong <strong>Windows PowerShell</strong> (phiên bản cũ, dành riêng cho Windows), thiết lập này sẽ tạo tệp UTF-8 có <strong>BOM</strong>.</li>



<li>Trong <strong>PowerShell 7</strong> (phiên bản đa nền tảng), mã hóa UTF-8 <strong>không có BOM</strong> là mặc định.</li>
</ul>



<h2 class="wp-block-heading">2. Đặt trong profile PowerShell</h2>



<p>Bạn có thể thêm các dòng mã trên vào tệp profile PowerShell của mình để thiết lập này có hiệu lực mỗi khi bạn mở PowerShell.</p>



<ul class="wp-block-list">
<li>Mở tệp profile bằng lệnh:</li>
</ul>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">notepad $PROFILE</pre>



<ul class="wp-block-list">
<li>Thêm dòng lệnh <code><strong>$PSDefaultParameterValues</strong></code> vào tệp, sau đó lưu lại.</li>
</ul>



<h2 class="wp-block-heading">3. Thay đổi mã hóa đầu ra tạm thời</h2>



<p>Nếu bạn chỉ muốn thay đổi mã hóa cho một lệnh duy nhất, bạn có thể sử dụng <code>Out-File</code> với tùy chọn <code>-Encoding</code> như sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">"nội dung" | Out-File -FilePath "tệp.txt" -Encoding utf8</pre>



<h2 class="wp-block-heading">4. Sử dụng trong tập lệnh</h2>



<p>Nếu bạn đang viết một tập lệnh PowerShell và muốn đảm bảo rằng tất cả các đầu ra được ghi bằng mã hóa UTF-8, bạn có thể thêm tùy chọn <code>-Encoding utf8</code> cho từng lệnh ghi tệp, hoặc sử dụng <code>$PSDefaultParameterValues</code> như đã nêu ở trên.</p>



<p>Với 4 cách trên thì bạn có thể đảm bảo rằng PowerShell sử dụng mã hóa UTF-8 cho đầu ra của mình. Hy vọng chia sẻ này sẽ giúp ích bạn khi dùng PowerShell.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-thay-doi-ma-hoa-dau-ra-mac-dinh-thanh-utf-8-trong-powershell/">Cách thay đổi mã hóa đầu ra mặc định thành UTF-8 trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/cach-thay-doi-ma-hoa-dau-ra-mac-dinh-thanh-utf-8-trong-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cách truyền nhiều tham số vào một hàm trong PowerShell</title>
		<link>https://nhatphuc.com/cach-truyen-nhieu-tham-so-vao-mot-ham-trong-powershell/</link>
					<comments>https://nhatphuc.com/cach-truyen-nhieu-tham-so-vao-mot-ham-trong-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Wed, 25 Sep 2024 08:32:45 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12375</guid>

					<description><![CDATA[<p>Khi làm việc với PowerShell, bạn có thể truyền nhiều tham số vào một hàm [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-truyen-nhieu-tham-so-vao-mot-ham-trong-powershell/">Cách truyền nhiều tham số vào một hàm trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Khi làm việc với PowerShell, bạn có thể truyền nhiều tham số vào một hàm mà không cần sử dụng dấu phẩy hoặc dấu ngoặc đơn. Dưới đây là cách thực hiện:</p>



<h2 class="wp-block-heading">Định nghĩa hàm</h2>



<p>Bạn có thể định nghĩa hàm với nhiều tham số bằng cách sử dụng từ khóa <code>param</code>. Đây là ví dụ về một hàm đơn giản nhận hai chuỗi ký tự làm tham số:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">function KiemTra-ThamSo {
    param (
        [string]$thamSo1,
        [string]$thamSo2
    )
    
    Write-Host "`$thamSo1 có giá trị: $thamSo1"
    Write-Host "`$thamSo2 có giá trị: $thamSo2"
}</pre>



<h2 class="wp-block-heading">Gọi hàm với nhiều tham số</h2>



<p>Bạn có thể gọi hàm này bằng cách truyền các tham số theo cách sau:</p>



<p><strong>Sử dụng tham số theo thứ tự vị trí:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">KiemTra-ThamSo "ABC" "DEF"</pre>



<p><strong>Kết quả:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$thamSo1 có giá trị: ABC
$thamSo2 có giá trị: DEF</pre>



<p><strong>Sử dụng tên tham số (đặc biệt khi không cần theo thứ tự):</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">KiemTra-ThamSo -thamSo2 "DEF" -thamSo1 "ABC"</pre>



<ol class="wp-block-list">
<li></li>
</ol>



<p>Kết quả sẽ giống như trên, dù bạn truyền tham số không theo thứ tự.</p>



<h2 class="wp-block-heading">Lưu ý quan trọng</h2>



<ul class="wp-block-list">
<li>Trong PowerShell, tham số được truyền bằng cách phân tách bởi khoảng trắng, không phải dấu phẩy như trong một số ngôn ngữ lập trình khác như C# hay Python.</li>



<li>Bạn không cần sử dụng dấu ngoặc đơn khi gọi hàm, trừ khi bạn đang xử lý một đối tượng cụ thể.</li>
</ul>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-truyen-nhieu-tham-so-vao-mot-ham-trong-powershell/">Cách truyền nhiều tham số vào một hàm trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/cach-truyen-nhieu-tham-so-vao-mot-ham-trong-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sửa lỗi: &#8220;Execution of scripts is disabled on this system&#8221; trong PowerShell</title>
		<link>https://nhatphuc.com/sua-loi-execution-of-scripts-is-disabled-on-this-system-trong-powershell/</link>
					<comments>https://nhatphuc.com/sua-loi-execution-of-scripts-is-disabled-on-this-system-trong-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Wed, 25 Sep 2024 08:26:27 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Khám phá]]></category>
		<category><![CDATA[Command Prompt]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12372</guid>

					<description><![CDATA[<p>Khi bạn cố gắng chạy một script PowerShell trên hệ thống và gặp thông báo [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/sua-loi-execution-of-scripts-is-disabled-on-this-system-trong-powershell/">Sửa lỗi: &#8220;Execution of scripts is disabled on this system&#8221; trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Khi bạn cố gắng chạy một script PowerShell trên hệ thống và gặp thông báo lỗi: &#8220;Execution of scripts is disabled on this system,&#8221; điều này thường liên quan đến chính sách thực thi (Execution Policy) của PowerShell. Dưới đây là một số phương pháp để khắc phục lỗi này.</p>



<h2 class="wp-block-heading">1. Hiểu về PowerShell Execution Policy</h2>



<p>PowerShell có các mức chính sách thực thi để bảo vệ hệ thống khỏi việc chạy các script không đáng tin cậy. Các mức chính sách phổ biến bao gồm:</p>



<ul class="wp-block-list">
<li><strong>Restricted</strong>: Chặn hoàn toàn việc chạy script.</li>



<li><strong>AllSigned</strong>: Yêu cầu tất cả các script phải được ký số bởi nhà phát triển đáng tin cậy.</li>



<li><strong>RemoteSigned</strong>: Cho phép chạy các script cục bộ không cần ký, nhưng các script tải từ Internet phải được ký.</li>



<li><strong>Unrestricted</strong>: Không yêu cầu chữ ký và có thể chạy bất kỳ script nào, nhưng sẽ có cảnh báo khi chạy script từ Internet.</li>
</ul>



<h2 class="wp-block-heading">2. Cách kiểm tra chính sách thực thi hiện tại</h2>



<p>Sử dụng lệnh sau để kiểm tra chính sách thực thi hiện tại của bạn:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Get-ExecutionPolicy</pre>



<h2 class="wp-block-heading">3. Thiết lập lại chính sách thực thi</h2>



<p>Nếu bạn cần thay đổi chính sách thực thi để có thể chạy các script, hãy chạy PowerShell với quyền Administrator và sử dụng một trong các lệnh sau:</p>



<ul class="wp-block-list">
<li>Đặt chính sách thành <strong>RemoteSigned</strong> (mức bảo mật hợp lý):<br><code><strong>Set-ExecutionPolicy RemoteSigned</strong></code></li>



<li>Đặt chính sách thành <strong>Unrestricted</strong> (cho phép chạy mọi script):<br><code><strong>Set-ExecutionPolicy Unrestricted</strong></code></li>



<li>Nếu bạn không có quyền quản trị, bạn có thể chỉ định chính sách cho người dùng hiện tại:<br><code><strong>Set-ExecutionPolicy RemoteSigned -Scope CurrentUser</strong></code></li>
</ul>



<h2 class="wp-block-heading">4. Bỏ qua chính sách thực thi cho một script cụ thể</h2>



<p>Nếu bạn không muốn thay đổi chính sách toàn hệ thống, bạn có thể bỏ qua chính sách thực thi chỉ cho một script bằng cách sử dụng lệnh sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-ExecutionPolicy Bypass -File script.ps1</pre>



<p>Lệnh này sẽ bỏ qua chính sách thực thi hiện tại chỉ cho lần chạy này.</p>



<h2 class="wp-block-heading">5. Sử dụng Group Policy Editor (gpedit.msc)</h2>



<p>Nếu bạn không thể thay đổi chính sách bằng các lệnh PowerShell, hãy sử dụng <strong>Group Policy Editor</strong> để cho phép chạy script:</p>



<ol class="wp-block-list">
<li>Nhấn <strong>Windows + R</strong>, nhập <code>gpedit.msc</code> và nhấn <strong>Enter</strong>.</li>



<li>Điều hướng đến: <strong>Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Components -> Windows PowerShell</strong>.</li>



<li>Kích hoạt <strong>Turn on Script Execution</strong> và chọn <strong>Allow all scripts</strong>.</li>
</ol>



<p>Lưu ý: Khi bạn dùng các chính sách như <strong>Unrestricted</strong> có thể làm giảm bảo mật hệ thống, đặc biệt là khi chạy các script từ nguồn không đáng tin cậy. Bạn nên sử dụng mức <strong>RemoteSigned</strong> khi có thể, và luôn kiểm tra nguồn gốc của các script trước khi chạy.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/sua-loi-execution-of-scripts-is-disabled-on-this-system-trong-powershell/">Sửa lỗi: &#8220;Execution of scripts is disabled on this system&#8221; trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/sua-loi-execution-of-scripts-is-disabled-on-this-system-trong-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sử dụng mã hóa UTF-8 (CHCP 65001) trong Command Prompt / Windows PowerShell</title>
		<link>https://nhatphuc.com/su-dung-ma-hoa-utf-8-chcp-65001-trong-command-prompt-windows-powershell/</link>
					<comments>https://nhatphuc.com/su-dung-ma-hoa-utf-8-chcp-65001-trong-command-prompt-windows-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Wed, 25 Sep 2024 08:20:20 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12369</guid>

					<description><![CDATA[<p>Việc sử dụng mã hóa UTF-8 trong Command Prompt và Windows PowerShell trên Windows 10 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/su-dung-ma-hoa-utf-8-chcp-65001-trong-command-prompt-windows-powershell/">Sử dụng mã hóa UTF-8 (CHCP 65001) trong Command Prompt / Windows PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Việc sử dụng mã hóa <strong>UTF-8</strong> trong <strong>Command Prompt</strong> và <strong>Windows PowerShell</strong> trên Windows 10 đôi khi gây ra sự bất tiện, đặc biệt là khi bạn cần hỗ trợ các ký tự quốc tế. Dưới đây là cách thực hiện và những lưu ý quan trọng.</p>



<h2 class="wp-block-heading">Vấn đề thường gặp khi sử dụng CHCP 65001</h2>



<p><strong>CHCP 65001</strong> chuyển đổi trang mã (code page) hiện tại sang UTF-8 trong <strong>Command Prompt</strong> hoặc <strong>PowerShell</strong>. Điều này có thể hữu ích cho việc xử lý các ký tự quốc tế. Tuy nhiên, sử dụng <strong>CHCP 65001</strong> thường dẫn đến một số vấn đề:</p>



<ol class="wp-block-list">
<li><strong>Ký tự hiển thị sai hoặc không hiển thị</strong>: Một số ứng dụng không hỗ trợ UTF-8 đúng cách có thể hiển thị ký tự không chính xác.</li>



<li><strong>Giới hạn nhập và xuất của ứng dụng cũ</strong>: Các ứng dụng cũ có thể không tương thích với mã hóa UTF-8 và gặp lỗi khi đọc hoặc ghi ký tự.</li>
</ol>



<h2 class="wp-block-heading">Cách thiết lập UTF-8 hệ thống</h2>



<p>Từ Windows 10, phiên bản 1903 trở đi, bạn có thể thiết lập hệ thống để sử dụng <strong>UTF-8</strong> làm mã hóa mặc định thông qua cài đặt <strong>System Locale</strong>. Thực hiện theo các bước sau:</p>



<ol class="wp-block-list">
<li>Mở <strong>Control Panel</strong> > <strong>Region</strong>.</li>



<li>Trong tab <strong>Administrative</strong>, nhấn <strong>Change system locale</strong>.</li>



<li>Chọn <strong>Beta: Use Unicode UTF-8 for worldwide language support</strong>.</li>



<li>Khởi động lại hệ thống để áp dụng thay đổi.</li>
</ol>



<h2 class="wp-block-heading">Cấu hình Windows PowerShell để hỗ trợ UTF-8</h2>



<p>Trong <strong>Windows PowerShell</strong>, ngoài việc thiết lập mã hóa UTF-8 qua <strong>CHCP 65001</strong>, bạn cần điều chỉnh biến <strong>$OutputEncoding</strong> để xuất ra UTF-8. Thêm dòng lệnh sau vào <strong>$PROFILE</strong> của bạn để áp dụng khi khởi động:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$OutputEncoding = [System.Text.UTF8Encoding]::new()</pre>



<p>Điều này đảm bảo rằng PowerShell sẽ xuất dữ liệu ra đúng mã hóa UTF-8, bao gồm cả đầu ra từ các lệnh và tương tác với các chương trình bên ngoài.</p>



<h2 class="wp-block-heading">Sử dụng Windows Terminal để hỗ trợ tốt hơn</h2>



<p>Microsoft đã phát triển <strong>Windows Terminal</strong>, hỗ trợ Unicode và UTF-8 tốt hơn so với <strong>Command Prompt</strong> truyền thống và <strong>PowerShell</strong>. Bạn có thể tải xuống <strong>Windows Terminal</strong> từ Microsoft Store nếu đang sử dụng Windows 10 phiên bản 1903 trở lên. Trong <strong>Windows Terminal</strong>, bạn có thể tùy chỉnh mã hóa đầu vào và đầu ra cho từng phiên làm việc, đảm bảo hỗ trợ tốt hơn cho các ký tự quốc tế.</p>



<h4 class="wp-block-heading">Thiết lập mã hóa UTF-8 tự động trong Command Prompt</h4>



<p>Nếu bạn muốn tự động thiết lập <strong>CHCP 65001</strong> mỗi khi mở <strong>Command Prompt</strong>, bạn có thể thêm lệnh này vào registry:</p>



<ol class="wp-block-list">
<li>Mở <strong>Registry Editor</strong> và điều hướng đến:
<ul class="wp-block-list">
<li><code><strong>HKEY_CURRENT_USER\Software\Microsoft\Command Processor</strong></code></li>
</ul>
</li>



<li>Thêm khóa <strong>AutoRun</strong> với giá trị:textSao chép mã<code>chcp 65001 >nul</code></li>
</ol>



<p>Điều này sẽ đảm bảo rằng <strong>CHCP 65001</strong> được thực hiện mỗi khi bạn mở <strong>Command Prompt</strong> mà không cần gõ lệnh thủ công.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/su-dung-ma-hoa-utf-8-chcp-65001-trong-command-prompt-windows-powershell/">Sử dụng mã hóa UTF-8 (CHCP 65001) trong Command Prompt / Windows PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/su-dung-ma-hoa-utf-8-chcp-65001-trong-command-prompt-windows-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cách lấy giá trị biến môi trường PATH trong PowerShell</title>
		<link>https://nhatphuc.com/cach-lay-gia-tri-bien-moi-truong-path-trong-powershell/</link>
					<comments>https://nhatphuc.com/cach-lay-gia-tri-bien-moi-truong-path-trong-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Linh Nguyễn]]></dc:creator>
		<pubDate>Wed, 25 Sep 2024 06:49:18 +0000</pubDate>
				<category><![CDATA[Phần mềm hay]]></category>
		<category><![CDATA[Học công nghệ]]></category>
		<category><![CDATA[PowerShell]]></category>
		<guid isPermaLink="false">https://nhatphuc.com/?p=12363</guid>

					<description><![CDATA[<p>Khi làm việc trên PowerShell, bạn có thể muốn kiểm tra giá trị của biến [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-lay-gia-tri-bien-moi-truong-path-trong-powershell/">Cách lấy giá trị biến môi trường PATH trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Khi làm việc trên PowerShell, bạn có thể muốn kiểm tra giá trị của biến môi trường PATH, một biến chứa danh sách các đường dẫn đến các thư mục mà hệ điều hành sẽ tìm kiếm để chạy các chương trình và ứng dụng. Bài viết này sẽ hướng dẫn các bạn các cách khác nhau để lấy giá trị đầy đủ của biến môi trường này trong PowerShell.</p>



<h2 class="wp-block-heading">Vấn đề thường gặp</h2>



<p>Khi bạn cố gắng lấy giá trị của biến PATH bằng cách sử dụng lệnh:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Get-ChildItem env:path</strong></p>
</blockquote>



<p>PowerShell chỉ hiển thị một phần của đường dẫn, phụ thuộc vào độ rộng của cửa sổ PowerShell. </p>



<figure class="wp-block-image size-full"><img decoding="async" width="959" height="63" src="https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell.webp" alt="Cách lấy giá trị biến môi trường PATH trong PowerShell 2" class="wp-image-12364" title="Cách lấy giá trị biến môi trường PATH trong PowerShell 2" srcset="https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell.webp 959w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-320x21.webp 320w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-768x50.webp 768w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-561x37.webp 561w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-265x17.webp 265w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-531x35.webp 531w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-364x24.webp 364w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-728x48.webp 728w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-608x40.webp 608w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-758x50.webp 758w, https://nhatphuc.com/wp-content/uploads/2024/09/path-env-powershell-731x48.webp 731w" sizes="(max-width: 959px) 100vw, 959px" /></figure>



<p>Ví dụ:</p>



<p>Nó không hiển thị toàn bộ giá trị của biến PATH, gây khó khăn khi bạn muốn xem tất cả các thư mục trong biến.</p>



<h2 class="wp-block-heading">Giải pháp</h2>



<p><strong>1. Sử dụng $env:path</strong></p>



<p>Một cách đơn giản nhất để xem toàn bộ giá trị biến PATH là sử dụng biến môi trường $env:path:</p>



<ol class="wp-block-list"></ol>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>$env:path</strong></p>
</blockquote>



<p>Lệnh này sẽ hiển thị toàn bộ giá trị của biến PATH mà không bị cắt ngắn. Tuy nhiên, nếu biến PATH quá dài, việc đọc danh sách các đường dẫn trên một dòng có thể trở nên khó khăn.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="960" height="36" src="https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-960x36.webp" alt="Cách lấy giá trị biến môi trường PATH trong PowerShell 3" class="wp-image-12365" title="Cách lấy giá trị biến môi trường PATH trong PowerShell 3" srcset="https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-960x36.webp 960w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-320x12.webp 320w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-768x29.webp 768w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-561x21.webp 561w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-265x10.webp 265w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-531x20.webp 531w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-364x14.webp 364w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-728x27.webp 728w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-608x23.webp 608w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308-758x28.webp 758w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134308.webp 963w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p><strong>2. Sử dụng echo</strong></p>



<p>Bạn cũng có thể sử dụng lệnh echo để in ra giá trị biến PATH:</p>



<ol start="2" class="wp-block-list"></ol>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>echo $env:path</strong></p>
</blockquote>



<p>Kết quả của lệnh này tương tự như lệnh $env:path</p>



<p><strong>3. Sử dụng Format-List</strong></p>



<p>Nếu bạn chỉ gặp vấn đề về hiển thị trên một dòng, bạn có thể định dạng lại kết quả bằng cách sử dụng cmdlet Format-List:</p>



<ol start="3" class="wp-block-list"></ol>



<p>Get-ChildItem env:path | Format-List *</p>



<p>Lệnh này sẽ hiển thị toàn bộ giá trị của biến PATH theo danh sách, giúp dễ dàng theo dõi các đường dẫn hơn.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="960" height="147" src="https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-960x147.webp" alt="Cách lấy giá trị biến môi trường PATH trong PowerShell 4" class="wp-image-12366" title="Cách lấy giá trị biến môi trường PATH trong PowerShell 4" srcset="https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-960x147.webp 960w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-320x49.webp 320w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-768x117.webp 768w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-561x86.webp 561w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-265x40.webp 265w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-531x81.webp 531w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-364x56.webp 364w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-728x111.webp 728w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-608x93.webp 608w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-758x116.webp 758w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-314x48.webp 314w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423-628x96.webp 628w, https://nhatphuc.com/wp-content/uploads/2024/09/2024-09-25_134423.webp 962w" sizes="auto, (max-width: 960px) 100vw, 960px" /></figure>



<p><strong>4. Sử dụng Select-Object</strong></p>



<p>Một cách khác là sử dụng Select-Object để lấy tất cả thuộc tính của biến PATH:</p>



<ol start="4" class="wp-block-list"></ol>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Get-ChildItem Env:\Path | Select-Object *</strong></p>
</blockquote>



<p>Hoặc bạn có thể lấy giá trị của biến trực tiếp:</p>



<p><strong>(Get-ChildItem Env:\Path).Value</strong></p>



<p>Kết quả của lệnh này tương tự như lệnh $env:path</p>



<p><strong>5. Hiển thị từng đường dẫn trên mỗi dòng</strong></p>



<p>Nếu bạn muốn hiển thị từng đường dẫn trong biến PATH trên các dòng riêng biệt, bạn có thể sử dụng lệnh sau:</p>



<ol start="5" class="wp-block-list"></ol>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>$env:Path -split &#8216;;&#8217;</strong></p>
</blockquote>



<p>Hoặc sử dụng phương pháp thay thế các dấu chấm phẩy bằng ký tự xuống dòng:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>$env:Path -replace &#8216;;&#8217;, &#8220;`n&#8221;</strong></p>
</blockquote>



<p>Cả hai lệnh này sẽ giúp bạn đọc danh sách đường dẫn trong biến PATH dễ dàng hơn, bằng cách tách chúng ra thành từng dòng riêng biệt.</p>



<p>Đây có lẽ là cách hay nhất để bạn dễ theo dõi.</p>



<p><strong>6. Các lựa chọn khác</strong></p>



<p>Bạn cũng có thể sử dụng các cách tiếp cận tương tự như:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>$env:PATH.Split(&#8216;;&#8217;)</strong></p>
</blockquote>



<p>Hoặc:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>$env:PATH.Replace(&#8216;;&#8217;, &#8220;`n&#8221;)</strong></p>
</blockquote>



<p>Hy vọng nội dung chia sẻ nho nhỏ này giúp bạn lấy và hiển thị giá trị của biến PATH, tùy thuộc vào nhu cầu của bạn dễ dàng. Dù bạn chỉ muốn hiển thị toàn bộ giá trị hay tách chúng ra từng dòng, những lệnh trên sẽ giúp bạn xử lý dễ dàng.</p>
<p>The post <a rel="nofollow" href="https://nhatphuc.com/cach-lay-gia-tri-bien-moi-truong-path-trong-powershell/">Cách lấy giá trị biến môi trường PATH trong PowerShell</a> appeared first on <a rel="nofollow" href="https://nhatphuc.com">Nhật Phúc</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhatphuc.com/cach-lay-gia-tri-bien-moi-truong-path-trong-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
