What is the purpose of Target in Get-ItemProperty of powershell - powershell

enter image description here
I am wondering what's the purpose of Target? If the type of Target is hash, how to set the key/value.
PS Set-ItemProperty -Path a.txt -Name Target -Value { "key1":"value1"}
At line:1 char:58
+ Set-ItemProperty -Path a.txt -Name Target -Value { "key1":"value1"}
+ ~~~~~~~~~
Unexpected token ':"value1"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken

As for your post title:
What is the purpose of Target in Get-ItemProperty of powershell
Use Get-Member to find out.
(Get-ItemProperty -Path 'D:\temp\ZenMusic.mp3' |
Get-Member) -match 'Target' |
Format-List
# Results
<#
TypeName : System.IO.FileInfo
Name : Target
MemberType : CodeProperty
Definition : System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Target{get=GetTarget;}
#>
As you can see, there is no setter for this property.

There is a target property for symlinks that's just a string, but it can't be set with set-itemproperty (Set accessor is unavailable).
New-Item -Path c -ItemType SymbolicLink -Value c:\ # elevated prompt
get-item c | % target | % gettype
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
get-itemproperty -path c -name target
target : {C:\}
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Users\admin\foo\c
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Users\admin\foo
PSChildName : c
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
Set-ItemProperty -Path c -Name Target -Value c:\users
Set-ItemProperty : Set accessor for property "Target" is unavailable.
At line:1 char:1
+ Set-ItemProperty -Path c -Name Target -Value c:\users
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Set-ItemProperty], SetValueException
+ FullyQualifiedErrorId : SetWithoutSetterFromCodeProperty,Microsoft.PowerShell.Commands.SetItemPropertyCommand

Related

Target in Get-ItemProperty of PowerShell and how to assign values

Get-ItemProperty retuns Target: {}, what is this Target and if it's hash, how to assign value to it
PS D:\xyz> Get-ItemProperty -Path a.txt | Format-list -Property * -Force
PSPath : Microsoft.PowerShell.Core\FileSystem::D:\xyz\a.txt
PSParentPath : Microsoft.PowerShell.Core\FileSystem::D:\xyz
PSChildName : a.txt
PSDrive : D
.............
BaseName : a
Target : {}
.............
Attributes : Archive
PS Set-ItemProperty -Path a.txt -Name Target -Value { "key1":"value1"}
At line:1 char:58
+ Set-ItemProperty -Path a.txt -Name Target -Value { "key1":"value1"}
+ ~~~~~~~~~
Unexpected token ':"value1"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
I tried to assign value like python and it did not work

Script block if and variable

my path var which right now I set directly is:
00257d48e8260671794a3c2cfc3fb2d1 My Pictures/Tony/Automatic Upload/Tony’s iPhone/2022-11-13 10-57-52.mov
20e2128b66ded28cc59eb764209ddacd My Pictures/Mom/Automatic Upload/Tony’s iPhone/2022-11-12 09-26-27.dng
44e2128b66de428cc59eb764209ddacd My Pictures/Mom/Automatic Upload/Tony’s iPhone/2024-11-12 09-26-27.dng
in my code this is getting set further up in a foreach loop foreach ($path in $diffmd5.value) {}
heres the get type
$path.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ScriptBlock System.Object
seems like there's an issue using the variable inside the if ?
if ($path | Select-String -Pattern 'My Pictures\/Mom\/') {
>> $path2 = Split-Path -Path $path -Leaf }
Split-Path : Cannot evaluate parameter 'Path' because its argument is specified as a script block and there is no
input. A script block cannot be evaluated without input.
At line:2 char:28
+ $path2 = Split-Path -Path $path -Leaf }
+ ~~~~~
+ CategoryInfo : MetadataError: (:) [Split-Path], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.SplitPathCommand
some issue here? https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_script_blocks?view=powershell-7#using-delay-bind-script-blocks-with-parameters

Why does powershell throw a path does not exist error?

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

Powershell: Expand-Archive: Cannot validate argument on parameter 'Path'

I'm trying to run a powershell command to extract all zip files into a folder to a new path.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased-music\incoming-unzipped' -Force
It looks like the command should work, but I get an error as such:
Expand-Archive : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:140
+ ... ter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:PSObject) [Expand-Archive], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Expand-Archive
Expand-Archive : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:140
+ ... ter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:PSObject) [Expand-Archive], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Expand-Archive
Expand-Archive : The path ' Directory: Z:\audio\music\purchased-music\incoming' either does not exist or is not a valid file system path.
At line:1 char:140
+ ... ter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: ( Directory: ...-music\incoming:String) [Expand-Archive], InvalidOperationException
+ FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive
The path and destination both do exist:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip
Directory: Z:\audio\music\purchased-music\incoming
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/6/2021 4:28 PM 38489484 Satsuma.zip
PS C:\Windows\System32\WindowsPowerShell\v1.0> dir Z:\audio\music\purchased-music\incoming-unzipped
Directory: Z:\audio\music\purchased-music\incoming-unzipped
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/16/2020 2:15 AM 20320 touch.txt
To expand on Olaf's helpful comment, this would work with braces as per powershell /?, or about_PowerShell_exe documentation:
PS C:\Windows\System32\WindowsPowerShell\v1.0> C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command {Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased-music\incoming-unzipped' -Force}
Since you're using PowerShell, you don't need to launch the exe to execute a command:
Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip |
Expand-Archive -DestinationPath 'Z:\audio\music\purchased-music\incoming-unzipped' -Force
(the PS C:\Windows\System32\WindowsPowerShell\v1.0> isn't relevant to code execution as you're specifying full paths Z:\audio\... etc - you could be running this from any location)

Powershell : New-Item -name < >

Scenario : a folder named "x" is created in a certain path
I'm trying to see if the following is possible :
if i can create a file named "x" in the same path where "x" folder is present without specifying any extension in the name parameter.
I used the command new-item -name x -path < > -force
Output: its throwing an error and am not able to create a file with same name
However , if i specify the extension ".txt" am able to do it.
I tried creating the file first and then the folder, but am getting another error. I used the force parameter to create the folder. There is no error , but when i check the path for the folder i dont see it ! The file is there though.
I would like to know why it is failing and is there a workaround for this?
FOLDER CREATED FIRST , THEN CREATING FILE Error
PS C:\Windows\system32> New-Item -name Test1 -path E:\ -ItemType directory
New-Item -name Test1 -path E:\ -ItemType file -force
Directory: E:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 16-05-2016 20:20 Test1
New-Item : Access to the path 'E:\Test1' is denied.
At line:3 char:1
+ New-Item -name Test1 -path E:\ -ItemType file -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (E:\Test1:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
FILE CREATED FIRST , THEN CREATING FOLDER ERROR
PS C:\Windows\system32>
New-Item -name Test2 -path E:\ -ItemType file -force
Directory: E:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 16-05-2016 20:26 0 Test2
PS C:\Windows\system32> New-Item -name Test2 -path E:\ -ItemType directory
New-Item : An item with the specified name E:\Test2 already exists.
At line:1 char:1
+ New-Item -name Test2 -path E:\ -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (E:\Test2:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
Files and folders on all major filesystems can't share the same name. A directory is a special type of file that from the user's perspective can "contain" other files.