# File and Directory Operations

## 1. Opening a File

Chúng ta có thể sử dụng "Get-Content" cmdlet để mở một tệp cụ thể trong PowerShell.&#x20;

```powershell
Get-Content -Path "C:\Path\To\Your\File\example.txt"
```

## 2. Reading File

Nếu bạn muốn đọc một số dòng cụ thể trong tệp, bạn có thể sử dụng tham số "`-TotalCount`". Ví dụ: bạn có thể sử dụng đoạn mã sau để đọc 5 dòng đầu tiên của tệp:

```powershell
Get-Content -Path "C:\Path\To\Your\File\example.txt" -TotalCount 5
```

## 3. Writing a File

Trong PowerShell, chúng ta có thể sử dụng các cmdlet “`Set-Content`”, “`Add-Content`” hoặc “`Out-File`” để ghi vào file.

### Set-Content

Cmdlet này cho phép bạn ghi nội dung vào một file cụ thể. Nếu file đã tồn tại, nó sẽ xóa nội dung hiện có và ghi nội dung mới vào. Ví dụ:

```powershell
Set-Content -Path "C:\Path\To\Your\File\example.txt" -Value "LetsDefend is Great"
```

### Add-Content

Cmdlet này cho phép bạn thêm nội dung vào cuối một file đã tồn tại. Nếu file không tồn tại, nó sẽ tạo file mới. Ví dụ:

{% code overflow="wrap" %}

```powershell
Add-Content -Path "C:\Path\To\Your\File\example.txt" -Value "LetsDefend is Great"
```

{% endcode %}

### Out-File

Cmdlet này cho phép bạn chuyển hướng đầu ra của một lệnh vào một file. Nếu file đã tồn tại, nó sẽ xóa nội dung và ghi đầu ra mới. Bạn có thể thêm nội dung vào cuối file hiện có bằng cách sử dụng tham số `-Append`. Ví dụ:

```powershell
Get-Process | Out-File -FilePath "C:\Path\To\Your\File\processes.txt"
```

Lệnh trên ghi danh sách các tiến trình hiện tại vào file `processes.txt`.

#### Ví dụ Sử dụng Các Tính Năng Này

Dưới đây là một ví dụ về cách sử dụng các tính năng này kết hợp với nhau:

```powershell
$filePath = "C:\example.txt"

# Đọc nội dung file
$content = Get-Content -Path $filePath
Write-Host "Nội dung gốc:"
Write-Host $content

# Thêm nội dung vào file
Add-Content -Path $filePath -Value "New line of text"

# Đọc nội dung mới
$newContent = Get-Content -Path $filePath
Write-Host "Nội dung đã cập nhật:"
Write-Host $newContent
```

Trong ví dụ này, đầu tiên chúng ta đọc nội dung ban đầu của file, sau đó thêm một dòng văn bản mới, và cuối cùng đọc và hiển thị nội dung đã được cập nhật.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/education/window-pe-.net/powershell/introduction-to-powershell/file-and-directory-operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
