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.
Related
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
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)
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
I've got a directory, C:\temp\test\, containing three DLL's I've called First.dll, Second.dll, Third.dll. I want to create sub-directories named after each of the DLL's.
This is what I've tried so far:
$dirName = "Tenth"
new-item $dirName -ItemType directory
That works. It created a sub-directory called "Tenth".
This also works:
(get-childitem -file).BaseName | select $_
It returns:
First
Second
Third
I've checked the type of the output from that command and it tells me "select $_" is of type System.String.
Now the bit that doesn't work:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
I get the following error repeated three times:
new-item : An item with the specified name C:\temp\test already exists.
At line:1 char:34
+ (get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (C:\temp\test:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
The current folder I'm executing the commands from is C:\temp\test\ .
I haven't been able to find any similar examples on the Internet to show me where I'm going wrong. Can anyone give me any pointers? Cheers.
Now the bit that doesn't work:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
This way, it works and doesn't need the ForEach-Object:
(dir -file).BaseName|ni -name{$_} -ItemType directory -WhatIf
$_ references each item as it comes along the pipeline so you will need to pipe to ForEach-Object for your line to work, like this:
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -ItemType directory}
This will create the item in the current powershell directory, you can also specify -Path if you want to create the folders somewhere else.
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -Path C:\MyFolder -ItemType directory}
New-Item accepts the parameter -Path to be piped and it can be an array of strings.
Then, you can create an object with a property Path that contains all the needed folders to be created
<#
# a simple array as example,
# but it could be the result of any enumerable,
# such as Get-ChildItem and stuff
#>
$FoldersToCreate = #('a', 'b', 'c')
# creates the folders a, b and c at the current working directory
[PSCustomObject]#{ Path = $FoldersToCreate } | New-Item -ItemType Directory
Or, alternatively :
$FoldersToCreate |
Select-Object #{ name = "Path"; expression = { "c:\testDir\$_" } } |
New-Item -ItemType Directory
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