如何使用PowerShell删除INI文件中的特定项?

问题描述 投票:1回答:1

我想删除INI文件中的特定项目。我的INI文件

[Information]
Name= Joyce
Class=Elementry
Age=10

我想删除Age=10

我尝试了这段代码,但我无法删除qazxsw poi的值qazxsw poi。

Age

我对INI文件的期望输出是:

10
powershell ini
1个回答
1
投票

Param( [parameter(mandatory=$true)]$FilePath, [parameter(mandatory=$true)] $a, [parameter(mandatory=$true)] $b, [parameter(mandatory=$true)] $c ) Import-Module PsIni $ff = Get-IniContent $FilePath $ff["$a"]["$b"] = "$c" $ff | Out-IniFile -FilePath $FilePath -Force 返回一个(嵌套的)有序哈希表,表示INI文件的结构。

要删除条目,您必须使用有序哈希表的[Information] Name=Joyce Class=Elementry 方法:

Get-IniContent

因此,您可以按如下方式修改脚本:

.Remove()

然后按如下方式调用它;请注意省略第4个参数,该参数请求删除条目:

# Read the INI file into a (nested) ordered hashtable.
$iniContent = Get-IniContent file.ini

# Remove the [Information] section's 'Age' entry.
$iniContent.Information.Remove('Age')

# Save the updated INI representation back to disk.
$iniContent | Out-File -Force file.ini
© www.soinside.com 2019 - 2024. All rights reserved.