powershell - 替换导入的CSV文件中的字符串

标签 powershell powershell-4.0

我需要导入CSV文件,然后将完整的用户名domain\username替换为username

以下几行有效,但我只收到修改后的用户名作为输出,而不是完整文件。

您能否提一些建议?

$TestFile = Import-Csv .\file.csv 
$NewFile = $TestFile | ForEach-Object {$_."Username" -replace 'domain\\',''}

最佳答案

您可以对字符串数据执行替换,然后使用ConvertFrom-Csv将其转换为对象。

$TestFile = (Get-Content .\file.csv) -replace 'domain\\',''
$NewFile = ConvertFrom-Csv $TestFile

关于powershell - 替换导入的CSV文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45963765/

相关文章:

typescript - 如何获取Powershell脚本输出fin Typescript文件?

powershell - 当管道文件时,Import-CSV 返回 null

powershell - 特定的 PowerShell 模块未自动加载

PowerShell - 将属性名称从 Pascal 大小写转换为带下划线的大写

powershell - 带有 utf-8 编码的 tee

powershell - Powershell将文件路径中最后出现的 '/'替换为 '.'

azure - 使用 Azure devops 中的 Azure powershell 任务向 Azure Keyvault 添加/获取 key

python - 通过 powershell 命令传递时字典项目不起作用

arrays - 数组在PowerShell中的Function之外没有输出

powershell - Powershell if语句