Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell中创建文件夹和文件_Powershell_Powershell 3.0 - Fatal编程技术网

在Powershell中创建文件夹和文件

在Powershell中创建文件夹和文件,powershell,powershell-3.0,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

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

$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 ' ') 
这将在文件中写入数组的所有元素,以空格分隔


更多解决方案:marosoaie已回答您的问题,但您也可以替换:

$myarray=@()

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

    $myarray += $x

}

创建一个从1到20的数字数组。简短示例:

PS C:\> 1..5
1
2
3
4
5

谢谢,但是我需要从循环中得到它,不管怎样,这是一项任务,谢谢
PS C:\> 1..5
1
2
3
4
5