PowerShell을 사용하여 파일에 데이터를 추가하는 방법

  1. PowerShell에서의 Add-Content 기본 구문
  2. PowerShell에서 Add-Content를 사용하여 파일에 텍스트 추가하기
  3. PowerShell에서 새로운 줄에 데이터를 추가하기 위해 (`n) 연산자 사용하기
  4. PowerShell에서 하나의 파일의 내용을 다른 파일에 추가하기 위해 Add-Content 명령 사용하기
  5. PowerShell에서 읽기 전용 파일에 데이터를 추가하기 위해 Add-Content 명령 사용하기
PowerShell을 사용하여 파일에 데이터를 추가하는 방법

PowerShell의 Add-Content 명령은 파일에 내용을 추가합니다. 우리는 명령에서 내용을 지정하거나 Get-Content를 사용하여 파일의 내용을 가져와 덧붙일 수 있습니다.

Add-Content cmdlet은 파일에 텍스트를 추가하거나 문자열을 파일에 추가합니다.

PowerShell에서의 Add-Content 기본 구문

Windows PowerShell의 Add-Content cmdlet은 파일에 내용을 추가하고 파일에 텍스트를 추가합니다.

Add-Content
[-Path] <string[]>
[-Value] <Object[]>
[-PassThru]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-NoNewline]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <string>]
[<CommonParameters>]

PowerShell에서 Add-Content를 사용하여 파일에 텍스트 추가하기

예를 들어, 디렉토리에 Get-DateFile.txt 텍스트 파일이 있습니다.

Get-DateFile.txt로 새 파일을 만들고 일부 테스트 데이터를 추가합니다.

Get-DataFile.txt:

Example illustration about appending text to
Add-Content -Path .\Get-DateFile.txt "End of file"

Add-Content cmdlet은 현재 디렉토리에서 -Path 매개변수로 지정된 파일의 끝에 End of file 문자열을 추가합니다.

출력:

Example illustration about appending text to End of file

PowerShell에서 새로운 줄에 데이터를 추가하기 위해 (`n) 연산자 사용하기

파일에 새로운 줄로 데이터를 추가하려면 새로운 줄 연산자(`n)를 사용하십시오.

Add-Content -Path .\Get-DateFile.txt "`nSecond line starts here..."

출력:

Example illustration about appending text to End of file

Second line starts here…

PowerShell에서 하나의 파일의 내용을 다른 파일에 추가하기 위해 Add-Content 명령 사용하기

Add-Content 명령은 하나의 파일 내용을 다른 파일에 추가합니다.

파일 내용을 읽고 변수에 할당한 후 목적지 파일에 내용을 씁니다.

Add-Content 명령은 파일이 존재하지 않을 경우 파일에 텍스트를 추가하면서 새 파일을 생성합니다.

# Read file contents to variable
$sourceFileContent = Get-Content -Path .\GetFileProperties.txt 

# This line will append the content of one file to another file
# If the file does not exist, the Add-Content command will create a new file
Add-Content -Path .\Destination_File.txt -Value $sourceFileContent

하나의 파일의 내용을 다른 파일에 추가하기 위해 PowerShell의 Get-Content cmdlet은 Path 매개변수로 지정된 파일의 내용을 가져옵니다.

그런 다음, 파일의 내용을 읽고 $sourceFileContent 변수에 저장합니다.

PowerShell의 Add-Content cmdlet은 -Value 매개변수로 지정된 소스 파일의 내용을 추가합니다.

Add-Content 명령은 파일이 존재하지 않을 경우 새 파일을 생성하고 내용을 복사합니다.

PowerShell에서 읽기 전용 파일에 데이터를 추가하기 위해 Add-Content 명령 사용하기

# Create a new file
New-Item -Path .\TempROFile.txt -ItemType File

# Set file as read-only
Set-ItemProperty -Path .\TempROFile.txt -Name IsReadOnly -Value $True 

# Get file details
Get-ChildItem -Path .\TempROFile.txt  

# Appends the line to file
Add-Content -Path .\TempROFile.txt -Value 'End of File' -Force

첫 번째 명령은 PowerShell의 New-Item cmdlet을 사용하여 새 파일을 생성합니다.

PowerShell의 Set-ItemProperty 명령은 지정된 파일의 IsReadOnly 속성을 true로 설정하는 데 사용됩니다.

PowerShell의 Get-ChildItem 명령은 Name, LastWriteTime, Lengthmode와 같은 지정된 파일 세부 정보를 가져옵니다.

Add-Content cmdlet은 -Path 매개변수로 지정된 읽기 전용 파일에 줄을 추가합니다.

튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

관련 문장 - PowerShell File