Reputation: 2861
I want to use new-item
to create a new file , but I have a unauthorizedAccessException , it seems I can't access the path I assign
here is my code
$Random_Serial = Invoke-Expression .\Generate-Random-Number.ps1
Write-Host $Random_Serial
New-Item -Path ..\Database -Name "$Random_Serial" -ItemType file
I try to use text name to replace variable name and it can create file successfully
$Random_Serial = Invoke-Expression .\Generate-Random-Number.ps1
Write-Host $Random_Serial
New-Item -Path ..\Database -Name "11" -ItemType file
But I still can't figure out what is the problem?
Upvotes: 1
Views: 2607
Reputation: 3096
Make sure that you have security permission the the path. The output file will need an extension, and Write-Host is generally used for console output. You can use Write-Output as an alternative. Also, Out-File is a better option than New-Item for what you are trying to accomplish.
Change
Write-Host $Random_Serial
New-Item -Path ..\Database -Name "11" -ItemType file
to:
write-output $Random_Serial | Out-File -Filepath C:\11.txt
Upvotes: 2