With PowerShell you can run a command like this
ni c:/foo/bar -type directory
and it will create foo and bar as necessary. However if you run
ni hklm:software/classes/firefoxhtml/shell/edit/command -type directory
all keys but the last must exist or an error will be generated. Can PowerShell generate the parent keys as needed?
I was just missing the -force parameter
New-Item hklm:software/classes/firefoxhtml/shell/edit/command -Force
Using -Force will also remove everything under the key if it already exists so a better option would be
if(!(Test-Path $path)){
New-Item $path -Force;
}
Related
I want to create an alias in Windows PowerShell to delete multiple folders from the command line.
To remove more than one item I call:
Remove-Item '.\Documents\*\Bin\' ,'.\Documents\*\Inter\' -Force -Recurse
I have tried to create the alias like this:
New-Alias -Name 'Clean-RCD' Remove-Item '.\Documents\*\Bin\' ,'.\Documents\*\Inter\' -Force -Recurse
Output:
New-Alias: A positional parameter cannot be found that accepts argument 'System.Object[]'.
Any idea how to define this alias correctly?
Unlike in bash, aliases in PowerShell are strict 1-to-1 command name mappings - no extra parameter arguments allowed.
You'll want to create a function instead:
function Clean-RCD {
Remove-Item -Path '~\Documents\*\Bin', '~\Documents\*\Inter\' -Force -Recurse
}
Use of ~ (which resolves to your home folder) over . is intentional - this way it'll still work if you've navigated to a different path
I am having issues when removing SymbolicLinks which I have created with New-Item:
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\"
I need to modify the link because it has the wrong -Target, which should be:
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI"
How to remove that link and assign a new one? Alternatively, how to update the target path of the existing link?
Calling Delete() on the corresponding DirectoryInfo object should do the trick:
(Get-Item C:\SPI).Delete()
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI"
If you want to change the target path of the existing symbolic link C:\SPI from "C:\Users\Chino\Dropbox (Reserve Membership)\" to "C:\Users\Chino\Dropbox (Reserve Membership)\SPI\" you do not need to delete the link beforehand. Simply including the -Force parameter to overwrite the link works for me in PowerShell 5.1 on Windows 10 Pro v1603:
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI" -Force
No way to update the symbolic link as far as I know. Gotta use CMD to remove symbolic link and you could then re-create it using your powershell script. You would do it like this in powershell.
cmd /c "rmdir C:\SPI"
I am looking for a simple (if possible) way in powershell to check if a directory contains at least 1 other directory.
$dir = '.' # specify the directory to investigate
$hasAnySubdir = (Get-ChildItem -Force -Directory $dir).Count -gt 0
-Directory (PSv3+) ensures that Get-ChildItem only enumerates sub-directories, and -Force ensures that even hidden subdirectories are included.
I am using these lines. Is there any way to shorten it or is this the best?
Objective is to delete the Refresh folder and all of its contents. The script works, but I'd like it to be as minimal as possible.
# Set var $Path to equal Refresh folder
$Path = "C:\data\Refresh"
# Find sub-folders of Refresh
Get-ChildItem -Path $path -Recurse | Remove-Item -Force -Recurse
# Delete Refresh top level folder itself
Remove-Item $Path -Force
Since you want to delete the folder and all its content don't bother with Get-ChildItem. Simply call Remove-Item with the path you want to delete.
$Path = 'C:\data\Refresh'
Remove-Item $Path -Recurse -Force
or just
Remove-Item 'C:\data\Refresh' -Recurse -Force
I'm working on learning Windows PS - but until than I need some help writing a quick command:
I have a directory of directories, each directory has a unique name.
I need to rename all files within each of these directories to their parents name! As follows:
Current Structure:
/pdfdirectory/pdf_title_directory/filename.pdf
/pdfdirectory/pdf_title_directory123/filename.pdf
After shell script:
/pdfdirectory/pdf_title_directory/pdf_title_directory.pdf
/pdfdirectory/pdf_title_directory123/pdf_title_directory123.pdf
Thanks!
With good faith that you are learning Powershell and will try out stuff:
gci .\test -recurse | ?{ -not $_.PsIsContainer } |
%{rename-item -path $_.fullname -newname ($_.Directory.Name + $_.Extension)}
Of course, the above will fail it there is more than one file in the directory.
To understand the above learn basic Powershell commands like gci ( alias for Get-ChildItem) and Powershell pipeline concepts.
It's worth noting that you can pipe the output from Get-ChildItem directly into Rename-Item, you don't need a foreach. Also, and this is the clever part, the -NewName parameter value can be a script block that yields the new name. This script block can use $_ to refer to the file object that is currently being processed.
So you could do something like this:
dir -Recurse -Include filename.pdf | ren -NewName { "$($_.Directory.Name).pdf" }
(I think it was Keith Hill that made me aware of this trick.)