Access to the path is denied PowerShell
To solve the Access to the path is denied PowerShell problem, you should make sure that:
- The Windows PowerShell is running as Administrator.
- The provided path is correct in
Add-Content
.
- The file is not located directly in the
c:\
drive, it should be inside a folder in the root drive.
If you need to create your file in c:\
drive directly, make sure that you have added Set-ExecutionPolicy "Unrestricted"
at the beginning of your PowerShell Script.
Add-Content: Access to the path is denied.
As per your PowerShell script, you are trying to create a directory, not a file then you have set a folder path
rather than a file path
in Add-Content and that will cause this error 'Add-Content : Access to the path is denied."
In your case, you must make sure that you have set the -Path
in Add-Content
to a valid file path. so try to change your PowerShell script to the following:
#Create a folder
$Path = "C:\myfolder"
New-Item -ItemType Directory -Path $Path
# Create a file
$FilePath = "$Path\file.csv"
New-Item -ItemType file -Path $FilePath
# Check if the path is exist
if(Test-path $FilePath)
{
#add a row in CSV to the correct file path
Add-Content -Path $FilePath -Value "First Name,Last Name, Modified Date"
}