Cómo agregar datos a un archivo usando PowerShell

  1. la Sintaxis Básica de Add-Content en PowerShell
  2. Usar Add-Content para Agregar Texto a un Archivo en PowerShell
  3. Usar el Operador (`n) para Agregar Datos en una Nueva Línea en PowerShell
  4. Usar el Comando Add-Content para Agregar el Contenido de un Archivo a Otro Archivo en PowerShell
  5. Usar el Comando Add-Content para Agregar Datos a un Archivo de Solo Lectura en PowerShell
Cómo agregar datos a un archivo usando PowerShell

El comando Add-Content en PowerShell agrega contenido a un archivo. Podemos especificar contenido en el comando o Get-Content para obtener el contenido del archivo para agregar.

El cmdlet Add-Content agrega texto al archivo o agrega una cadena al archivo.

la Sintaxis Básica de Add-Content en PowerShell

El cmdlet Add-Content en Windows PowerShell agrega contenido al archivo y agrega texto al archivo.

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

Usar Add-Content para Agregar Texto a un Archivo en PowerShell

Por ejemplo, tienes el archivo de texto Get-DateFile.txt en el directorio.

Crea un nuevo archivo con Get-DateFile.txt, y agrega algunos datos de prueba.

Get-DataFile.txt:

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

El cmdlet Add-Content agrega la cadena End of file al final del archivo especificado por el parámetro -Path en el directorio actual.

Salida:

Example illustration about appending text to End of file

Usar el Operador (`n) para Agregar Datos en una Nueva Línea en PowerShell

Para agregar datos a un archivo en una nueva línea, usa el operador de nueva línea (`n).

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

Salida:

Example illustration about appending text to End of file

Second line starts here…

Usar el Comando Add-Content para Agregar el Contenido de un Archivo a Otro Archivo en PowerShell

El comando Add-Content agregará el contenido de un archivo a otro.

Leerá y asignará el contenido del archivo a una variable y escribirá el contenido en el archivo de destino.

El comando Add-Content creará un nuevo archivo si el archivo no existe mientras agrega el texto al archivo.

# 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

Para agregar el contenido de un archivo a otro archivo, el cmdlet Get-Content en PowerShell obtiene el contenido del archivo especificado por el parámetro Path.

Luego, lee el contenido del archivo y lo almacena en una variable $sourceFileContent.

El cmdlet Add-Content en PowerShell agrega el contenido del archivo fuente especificado en el parámetro -Value.

El comando Add-Content creará un nuevo archivo si el archivo no existe y copiará el contenido.

Usar el Comando Add-Content para Agregar Datos a un Archivo de Solo Lectura en PowerShell

# 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

El primer comando crea un nuevo archivo usando el cmdlet New-Item de PowerShell.

El comando Set-ItemProperty en PowerShell se usa para establecer la propiedad IsReadOnly en verdadero para el archivo especificado.

El comando Get-ChildItem en PowerShell obtiene detalles del archivo especificado como Name, LastWriteTime, Length y mode.

El cmdlet Add-Content agrega una línea a un archivo de solo lectura especificado por el parámetro -Path.

¿Disfrutas de nuestros tutoriales? Suscríbete a DelftStack en YouTube para apoyarnos en la creación de más guías en vídeo de alta calidad. Suscríbete
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

Artículo relacionado - PowerShell File