powershell:add-content

本文介绍了使用add-content命令在文件中添加内容的方法,包括其主要参数、具体用法和一些实用的例子。文章还详细解释了如何通过add-content命令向特定类型的文件添加内容,并且提供了在不同场景下的应用案例。

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

add-content命令,主要是向文件中添加内容

  • 主要参数有两个,-path用来指定路径,-value用来指定要添加的内容,暂时只知道这两个参数
  • 下面是例子
    • add-content -path *.txt -exclude help* -value "END" 
      这个命令是向在当前目录的所有txt文件添加"END"这个内容,但是以help开关的txt文件除外,-exclude 用来排除不符合要求的文件
    • add-content -Path file1.log, file2.log -Value (get-date) -passthru 
      这个命令是向fiel1.log和file2.log文件中添加日期,日期对象通过get-date这个命令获取,如果-value的值是对象,则用括号括起来。加上-passthru参数,会把get-date获取的对象输入到终端上。
    • add-content -path monthly.txt -value (get-content c:\rec1\weekly.txt) 
      这个命令是把当前目录的weekly的内容加入到monthly.txt这个文件中,是从末尾开始插入,而不是覆盖. get-content是获取文件内容的命令,加括号的作用是保证在执行add-content的时候,get-content已经执行完毕
    • add-content -value (get-content test.log) -path C:\tests\test134\logs\test134.log 
      这个命令的作用是先获取文件的内容,再把这个内容完全得到一个新文件中,test134.log可以不存在,add-content命令会创建这个文件

转载于:https://www.cnblogs.com/popping57/p/3208152.html

PS C:\Users\Administrator> # 创建永久别名(添加到 PowerShell 配置文件) >> $profileContent = @" >> # 添加 curl 别名 >> function Invoke-Curl { >> & "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" @args >> } >> Set-Alias -Name realcurl -Value Invoke-Curl >> "@ >> >> # 创建配置文件(如果不存在) >> if (-not (Test-Path $PROFILE)) { >> New-Item -Path $PROFILE -ItemType File -Force >> } >> >> # 添加内容到配置文件 >> $profileContent | Add-Content -Path $PROFILE >> >> # 立即加载配置文件 >> . $PROFILE >> >> # 现在可以使用 realcurl 命令 >> realcurl --version >> 目录: C:\Users\Administrator\Documents\WindowsPowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2025/8/12 18:52 0 Microsoft.PowerShell_profile.ps1 curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd PS C:\Users\Administrator> # 临时删除 PowerShell 的 curl 别名 >> Remove-Item Alias:curl -ErrorAction SilentlyContinue >> >> # 现在可以直接使用 curl >> curl --version >> Warning: C:\Users\Administrator\.curlrc:2: warning: '--compressed' the Warning: installed libcurl version doesn't support this curl 8.4.0 (Windows) libcurl/8.4.0 Schannel WinIDN Release-Date: 2023-10-11 Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp Features: AsynchDNS HSTS HTTPS-proxy IDN IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI threadsafe Unicode UnixSockets PS C:\Users\Administrator> # 验证真正的 curl 安装 >> & "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" --version >> >> # 预期输出: >> # curl 8.15.0 (x86_64-pc-win32) ... >> # Release-Date: 2025-07-16 >> # Protocols: dict file ftp ftps gopher gophers http https ... >> curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd PS C:\Users\Administrator> # 使用真正的 curl 下载文件 >> & "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" -O https://example.com/index.html >> >> # 或者使用别名(如果已设置) >> realcurl -O https://example.com/index.html >> PS C:\Users\Administrator> # 编辑 PowerShell 配置文件 >> $profileContent = @" >> # 添加自定义 curl 别名 >> function Invoke-Curl { >> & "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" @args >> } >> Set-Alias -Name realcurl -Value Invoke-Curl >> "@ >> >> # 创建/更新配置文件 >> if (-not (Test-Path $PROFILE)) { >> New-Item -Path $PROFILE -ItemType File -Force >> } >> $profileContent | Add-Content -Path $PROFILE >> >> # 重新加载配置 >> . $PROFILE >> >> # 使用测试 >> realcurl --version >> realcurl https://example.com >> curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> PS C:\Users\Administrator> # 创建 curl.cmd 包装器 >> @" >> @echo off >> "E:\curl-8.15.0_4-win64-mingw\bin\curl.exe" %* >> "@ | Out-File "$env:SYSTEMROOT\System32\curl.cmd" -Encoding ASCII >> >> # 测试 >> curl --version >> Warning: C:\Users\Administrator\.curlrc:2: warning: '--compressed' the Warning: installed libcurl version doesn't support this curl 8.4.0 (Windows) libcurl/8.4.0 Schannel WinIDN Release-Date: 2023-10-11 Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp Features: AsynchDNS HSTS HTTPS-proxy IDN IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI threadsafe Unicode UnixSockets PS C:\Users\Administrator>
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值