powershell - 在 Powershell 中创建文件夹和文件

标签 powershell powershell-3.0

我想编写一个脚本来创建一个不存在的目录并在该文件夹中创建一个文件。该脚本还应该能够为该文件分配一个值。以下是我的代码,但它没有将值分配给文件。在本例中,我尝试将数组插入到文件中。

$myarray=@()

for($x=0;$x -lt 20; $x++) {

$myarray += $x

}

New-Item -Path C:\temp -Force -Name myarray.log -ItemType File -Value $myarray 

当我打开文件 myarray.log 时,我看到的只是“System.Object[]”,而不是数组。这里需要一些帮助。

最佳答案

它写的是“System.Object[]”,因为这就是 $myarray.ToString() 的输出。 一种解决方案是:

New-Item -Path C:\temp -Force -Name myarray.log -ItemType File -Value $($myarray -join ' ') 

这会将数组的所有元素写入文件中,并用空格分隔。

更多解决方案在这里:How do I convert an array object to a string in PowerShell?

关于powershell - 在 Powershell 中创建文件夹和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720667/

相关文章:

mysql - 在电子邮件中没有查询结果

powershell - 非终止错误处理-if $ Error.count和$ ErrorActionPreference = 'stop'之间的区别

powershell - 如何使用 PowerShell 获取域的最新 SSL 证书?

.net - 使用本地服务器上的 powershell 脚本应用服务包(.msu 文件)更新

powershell - 将包含 PowerShell 对象表示法的文本文件转换为 CSV

azure - 从 azure blob 下载文件名中带有日期的文件,无论日期之前的名称如何

powershell - 运行 Get-ADGroupMember 时出现延迟

powershell - 使用文件夹 GUID 在 Powershell 中获取任何特殊文件夹路径

powershell 比较两个文件并用侧面指示器列出它们的列作为匹配/不匹配

windows - 使用脚本将 key 发送到 Tableau