Powershell commands详解之Function "add-content/ac"

本文介绍Powershell中add-content命令的使用方法,通过不同示例展示如何向指定文件追加内容,包括普通文本、多国语言字符、当前日期等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

add-content, alias 为ac

帮助:

  可以通过get-help add-content, help ac, 或者help ac -full来得到帮助。

用法:

  add-content <path> <content>, path 为你需要写入内容的目的文件,如"c:/new.txt", content 为你要写入的内容。

用途:

  向指定文件写入内容。

例子 1:

  add-content "c:/new.txt" "Powershell is powerful!"

  将文本"Powershell is powerful!"写入c盘下的new.txt末尾,不会覆盖已存在的内容。如果new.txt不存在,ps自动新建。两个参数的双引号都不是必须的。你可以写为add-content c:/new.txt "Powershell is powerful!", 注意后面"Powershell is powerful!”中间有空格,所以需要双引号包起来作为一个整体传入。

例子 2:

   add-content -path *.txt -exclude help* -value "END"

   将“END” 写入所有当前目录下的txt文件中,以help命名开头的文件除外。

例子 3:

   add-content -path file1.log, file2.log -value (get-date) -passthru

   将当前日期时间写入file1.log,file2.log,同时打印当前日期(-passthru 在无参数的情况下会将值传递给console)

   注意:

    (get-date)一定要用括号括起来,让get-date作为command进行调用,否则get-date会被当成expression计算,最终解释为字符串"get-date".

例子 4:

    add-content -path monthly.txt -value (get-content c:/temp/weekly.txt)

    将weekly.txt的内容添加到monthly.txt的末尾。

例子 5:

    add-content -value (get-content test.log) -path C:/tests/test134/logs/test134.log

    将test.log的内容添加到test134.log文件中去。如果目标文件不存在,自动创建,如果上层目录不存在,自动创建。

例子 6:

    add-content chinese.txt -encoding "UTF8" -value "中文"

    利用"UTF8"编码将多国语言文字写入目标文件中。目前能接受的Encoding参数为

        Unknown,String,Unicode ,Byte,BigEndianUnicode,UTF8,UTF7,Ascii

  

英文帮助文档如下:

NAME
    Add-Content
   
SYNOPSIS
    Adds content to the specified items, such as adding words to a file.
   
   
SYNTAX
    Add-Content [-Credential <PSCredential>] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | Ascii
    }] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Include <string[]>] [-PassThru] [-LiteralPath] <string[]> [-Value] <Obje
    ct[]> [-confirm] [-whatif] [<CommonParameters>]
   
    Add-Content [-Credential <PSCredential>] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | Ascii
    }] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Include <string[]>] [-PassThru] [-Path] <string[]> [-Value] <Object[]> [
    -confirm] [-whatif] [<CommonParameters>]
   
   
DETAILED DESCRIPTION
    The Add-Content cmdlet appends content to a specified item or file. You can specify the content by typing the content in the co
    mmand or by specifying an object that contains the content.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值