Why does powershell throw a path does not exist error? - powershell

Why does powershell throw an error when trying to create or modify a registry key from the docs I used
Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmployees" -Value 823
The error I get
Set-ItemProperty : Cannot find path 'HKLM:\Software\ContosoCompany' because it does not exist.
At line:1 char:1
+ Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKLM:\Software\ContosoCompany:String) [Set-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand
I am running the powershell as an admin and I tried to modify existing keys and also tried to create new ones but I still get this error

HKLM is the name of a 'drive'. Move there first
Set-Location HKLM:
Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmployees" -Value 823

Related

Is there an error-less PowerShell equivalent to: tar cfz hierarchy.tgz hierarchy

In GNU/Linux, I can just do this:
tar cfz hierarchy.tgz hierarchy
and it just works, even if some of the items in hierarchy are being used by other processes.
In Windows, I can right-click on a folder and send it to a compressed zip file, so long as it's in another place, like the Desktop, and that works.
But in Windows PowerShell, something you would think would be so simple, isn't:
PS E:\> Compress-Archive -Path e:\lib -DestinationPath .\e-lib-all.zip
ZipArchiveHelper : The process cannot access the file
'E:\lib\company\data\data-azure-java\1.0.1\data-azure-java-1.0.1.jar' because it is being used by another process.
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:697
char:30
+ ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (E:\lib\company...-java-1.0.1.jar:String) [Write-Error], IOException
+ FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper
New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:808
char:38
+ ... $srcStream = New-Object System.IO.BinaryReader $currentFileStream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I asked a coworker, what's the PowerShell equivalent of the GUI way that just works? He thought what the GUI does in the background is create a copy using service level rather than user permissions, and then zips that.
Is there a PowerShell way to do this? I've tried some other things that did not work, namely:
Copy-Item -Path "E:\lib" -Force -PassThru | `
Get-ChildItem | `
Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Get-ChildItem -Recurse -Path $sourcePath | Compress-Archive -DestinationPath $destinationPath
Get-ChildItem -Recurse -Path "E:\lib" | Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Yes, I looked at previous SO post Compress-Archive error: PermissionDenied but it didn't help (I got the same errors).

Object or property already exists error when trying to export-certificate from root store

I'm trying to export my entire Cert:\CurrentUser\Root\ certificate store to an .SST file. What am I doing incorrectly?
In an administrator's powershell - I'm using:
Get-ChildItem -Path Cert:\CurrentUser\Root\ | Export-Certificate -FilePath C:\certs\test.sst -Type SST.
It returns:
Export-Certificate : The object or property already exists. (Exception
from HRESULT: 0x80092005) At line:1 char:46
+ ... User\root | Export-Certificate -FilePath c:\certs\allcertasdfs.sst -T ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Export-Certificate], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.CertificateServices.Commands.ExportCertificateCommand

Reading values from offline registry file

I am trying to read an offline registry file:
$product_name = Get-ItemProperty -Path "C:\temp\RegistryHives\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -name ProductName | Select-Object -ExpandProperty ProductName
Getting the below error. What would be the right way to access the offline registry file keys and values?
Get-ItemProperty : Cannot find path 'C:\temp\RegistryHives\SOFTWARE\Microsoft\Windows NT\CurrentVersion' because it
does not exist.
At line:1 char:17
+ ... duct_name = Get-ItemProperty -Path "C:\temp\RegistryHives\SOFTWARE\Mi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\Registr...\CurrentVersion:String) [Get-ItemProperty], ItemNotFo
undException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
You need to load the reg hive before accessing it. reg.exe can load hives with the load command`
Reg.exe load 'HKLM\TempHive' C:\temp\RegistryHives\SOFTWARE
$product_name = Get-ItemProperty -Path "HKLM:\TempHive\Microsoft\Windows NT\CurrentVersion" -name ProductName | Select-Object -ExpandProperty ProductName
Reg.exe unload 'HKLM\TempHive'

How to grant access to create a new file?

With a PowerShell script I try to create a new file as follows:
New-Item -Path $LogDir -Value $LogName -Force -ItemType File
which is refused with the following error messsage:
New-Item : Access to the path 'D:\Testing\Data\Powershell\Log' is denied.
At D:\Testing\Data\Powershell\LoadRunner\LRmain.ps1:27 char:9
+ New-Item <<<< -Path $LogDir -Value $LogName -Force -ItemType File
+ CategoryInfo : PermissionDenied: (D:\Testing\Data\Powershell\Log:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
I am executing the script in an Administrator PowerShell 2.0 window on Windows Server 2008. The directory exists, but how can I fix this error?
I'll add #PetSerAl's answer so we can close this question.
You've used the wrong parameter for the filename. Replace -Value with -Name, ex:
New-Item -Path $LogDir -Name $LogName -Force -ItemType File

Access to path denied while creating new file on first iteration of loop

I have a strange problem when writing data to a new file. I have a list of files in a directory that contains data I'm parsing and returning data with the Create-VMwareconf() function. This returns the data in a hashtable that I've assigned to $t. I've pulled the desired folder and filename from the $t function however each time I begin the loop I get the following error for the initial folder creation, the second and third work fine. Interestingly enough the data that should be in the first file is present in the second folder.
If I run the script again it generates all three objects however the sequence of the of the data in the file matching the file name is incorrect.
Any help would be appreciated in how to stop the following error;
$e = (Get-Childitem ".\a\*\*.ini")
Set-Location "C:\WindowsRoot\vmwareconfigfiles\"
ForEach($d in $e){
$vmwaredirectory = New-item -type directory -path .\ -name $dd -Force
$vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force
$t = Create-VMwareconf($d)
$dd = $t.Value["0"]
#Write contents to new file
$t | Out-File $vmwarefile
}
Error received on initial run;
New-Item : Access to the path 'C:\WindowsRoot\vmwareconfigfiles' is denied.
At C:\WindowsRoot\parsedisrec.ps1:93 char:15
+ $vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\WindowsRoot\vmwareconfigfiles:String) [New- Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At C:\WindowsRoot\parsedisrec.ps1:97 char:15
+ $t | Out-File $vmwarefile
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand
New-Item -Type file fails during the first iteration, because $dd isn't yet initialized then, so you're trying to create a file with the same name as the current directory. You'd get the the same result if you used $null (or even .) as the name:
PS C:\> New-Item -Type file -Path 'C:\some\where' -Name $null -Force
New-Item : Access to the path 'C:\some\where' is denied.
At line:1 char:1
+ New-Item -Type file -Path 'C:\some\where' -Name $null -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\some\where:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
PS C:\> New-Item -Type file -Path 'C:\some\where' -Name '.' -Force
New-Item : Access to the path 'C:\some\where' is denied.
At line:1 char:1
+ New-Item -Type file -Path 'C:\some\where' -name '.' -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\some\where\.:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
Change this:
ForEach($d in $e){
$vmwaredirectory = New-item -type directory -path .\ -name $dd -Force
$vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force
$t = Create-VMwareconf($d)
$dd = $t.Value["0"]
into this:
ForEach($d in $e){
$t = Create-VMwareconf($d)
$dd = $t.Value["0"]
$vmwaredirectory = New-item -type directory -path .\ -name $dd -Force
$vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force