Powershell 向多个Notes.ini文件添加行

Powershell 向多个Notes.ini文件添加行,powershell,ini,Powershell,Ini,我有一个PowerShell脚本,我想运行它,在每个INI文件的末尾添加两行“IM_DISABLED”和“IM_NO_SETUP=1” 我想在一个文件夹H:\test上运行它,该文件夹的每个子文件夹都包含一个文件notes.ini 我有以下PowerShell,它使用-replace工作。但是,我无法使用replace,只需要在每个INI的末尾添加两行新行 $places = 'h:\test' $places | Get-ChildItem -Recurse -Include Notes.in

我有一个PowerShell脚本,我想运行它,在每个INI文件的末尾添加两行“IM_DISABLED”和“IM_NO_SETUP=1”

我想在一个文件夹
H:\test
上运行它,该文件夹的每个子文件夹都包含一个文件
notes.ini

我有以下PowerShell,它使用
-replace
工作。但是,我无法使用replace,只需要在每个INI的末尾添加两行新行

$places = 'h:\test'
$places | Get-ChildItem -Recurse -Include Notes.ini | ForEach-Object {
    (Get-Content $_) -replace 'KitType=1', "`nKitType=1`r`nIM_DISABLED=1`r`nIM_NO_SETUP=1" |
        Set-Content $_
    'Processed: {0}' -f $_.FullName
}

如果您有PowerShell v3或更新版本,只需使用
添加内容

$places = 'h:\test'
$lines  = 'IM_DISABLED', 'IM_NO_SETUP=1'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  Add-Content $_.FullName -Value $lines
  'Processed: {0}' -f $_.FullName
}
$places = 'h:\test'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  $f = $_.FullName
  (Get-Content $f), 'IM_DISABLED', 'IM_NO_SETUP=1' | Set-Content $f
  'Processed: {0}' -f $f
}
在早期版本中,您可以使用带有参数
-Append
Out File
。请注意,如果文件不是Unicode编码的,还需要定义编码

$places = 'h:\test'
$lines  = 'IM_DISABLED', 'IM_NO_SETUP=1'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  Out-File $_.FullName -InputObject $lines -Append -Encoding Ascii
  'Processed: {0}' -f $_.FullName
}
另一个选项是创建一个数组,其中包含
Get Content
输出和要添加的行,并使用
Set Content
将其写回文件:

$places = 'h:\test'
$lines  = 'IM_DISABLED', 'IM_NO_SETUP=1'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  Add-Content $_.FullName -Value $lines
  'Processed: {0}' -f $_.FullName
}
$places = 'h:\test'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  $f = $_.FullName
  (Get-Content $f), 'IM_DISABLED', 'IM_NO_SETUP=1' | Set-Content $f
  'Processed: {0}' -f $f
}

如果您有PowerShell v3或更新版本,只需使用
添加内容

$places = 'h:\test'
$lines  = 'IM_DISABLED', 'IM_NO_SETUP=1'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  Add-Content $_.FullName -Value $lines
  'Processed: {0}' -f $_.FullName
}
$places = 'h:\test'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  $f = $_.FullName
  (Get-Content $f), 'IM_DISABLED', 'IM_NO_SETUP=1' | Set-Content $f
  'Processed: {0}' -f $f
}
在早期版本中,您可以使用带有参数
-Append
Out File
。请注意,如果文件不是Unicode编码的,还需要定义编码

$places = 'h:\test'
$lines  = 'IM_DISABLED', 'IM_NO_SETUP=1'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  Out-File $_.FullName -InputObject $lines -Append -Encoding Ascii
  'Processed: {0}' -f $_.FullName
}
另一个选项是创建一个数组,其中包含
Get Content
输出和要添加的行,并使用
Set Content
将其写回文件:

$places = 'h:\test'
$lines  = 'IM_DISABLED', 'IM_NO_SETUP=1'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  Add-Content $_.FullName -Value $lines
  'Processed: {0}' -f $_.FullName
}
$places = 'h:\test'
Get-ChildItem $places -Recurse -Include Notes.ini | ForEach-Object {
  $f = $_.FullName
  (Get-Content $f), 'IM_DISABLED', 'IM_NO_SETUP=1' | Set-Content $f
  'Processed: {0}' -f $f
}