Multiple values for a powershell alais - powershell

I want to create an alias that will display my home directory and all of it's sub-directories.
The alias that I can create is-
new-alias -name myd -value get-childitem
But I need to display both the home directory and sub directories using one command. Can I assign multiple values for an alias?

You should create a function and do that:
function myd {Get-ChildItem -Recurse }
Then alias that if you wish:
New-Alias -Name myname -Value myd
You can directly call the "myd" also.

Related

Need PowerShell alias for cargo commands [duplicate]

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

How do I add HKCU Regkeys value for each current user in powershell script?

I want a powershell script to run each time a user login to Windows by placed in: Shell:common startup.
This script must add about 50 Regkey's in HKCU, which is setting/path for Presetfolders for a application.
I want to use Powershell and have tried this command adding the RegKey (This command needs to be repeated for each 50 regkeys!):
New-ItemProperty -Path 'HKCU:\Software\AppName' -Name 'PresetFolder' -PropertyType String -Value '$env:userprofile\Documents\AppName\Presets1' -Force
New-ItemProperty -Path 'HKCU:\Software\AppName' -Name 'PresetFolder' -PropertyType String -Value '$env:userprofile\Documents\AppName\Presets2' -Force .......
When using "$env:userprofile" instead of c:\Users\MyUserProfile\Documents\.... the -value in the RegKey will be: "$env:userprofile\Documents\NewFolder\Presets" and not as wanted: "c:\Users\MyUserProfile\Documents\NewFolder\Presets".
I need a Variable for each userprofile!
Alternatively I can after Program installation by using admin-account, I can exported all RegKey's as a .reg-file. Before using the powershell-script to merge the RegKeys everytime a user is logging in Windows, I now need to search and replace the value of the path (-Value) from AdminUserProfil-path into a variable for each user running the script.
Part of the Reg-file:
[HKEY_CURRENT_USER\Software\AppName\Version]
"HelpDocPath"="C:\Users\\AdminUserprofiles\\Documents\\AppName\\Version\\HTML Help\\en"
"ExciterCacheInstallPath"="C:\\Program Files\\AppName\\Version\\Exciter Cache"
"DSPResourceFilesInstallPath"="C:\\Program Files\\AppName\\Version/Resources"
"InstallPath"="C:\\Program Files\\AppName\\InstallFolder"
"PresetFolder"="C:\\Users\\AdminUserprofiles\\Documents\\AppName\\Version\\Presets\\Global Presets"\
Hope anyone can help?
What do I need to type for the right path, so each user will have there own path? Do I need a variable fo rusers or..?
Thank you.
Define $env:USERPROFILE as a variable so you can call it, otherwise PS will just output what you have typed, which is what is happening in this case.
$path = "$env:USERPROFILE"
New-ItemProperty -Path 'HKCU:\Software\AppName' -Name 'PresetFolder' -PropertyType String -Value '$path\Documents\AppName\Presets1' -Force

How to create PS alias for EXE with many path parameters?

Why such alias doesn't work? In my opinion there is problem with duplicated quotation marks.
New-Alias -Name "chrome" -Value ""C:\Program Files
(x86)\Google\Chrome\Application\chrome.exe"
--user-data-dir="%APPDATA%\Google\Chrome\User Data" --disk-cache-dir="%LocalAppData%\Google\Chrome\User Data""
To add to #boxdog's comment, pointing to the alias documentation, you must point your alias to a function if you want parameters.
Consider adding this to your $profile to accomplish your goal:
function Start-Chrome {
$argList = #(
"--user-data-dir=`"$Env:AppData\Google\Chrome\User Data`""
"--disk-cache-dir=`"$Env:LocalAppData\Google\Chrome\User Data`""
)
& "${Env:ProgramFiles(x86)}\Google\Chrome\Application\chrome.exe" #argList
}
New-Alias -Name chrome -Value Start-Chrome

Create Directory Using Partial Path

Problem
In PowerShell, is it possible to create a new file directory using a partial path that has wildcards (Ex: C:\*\SomeFolder\MyBackup)? If so, how?
Details
I am working with PowerShell to create an application, and one part of that application has the user designate a backup directory. This directory could be an exact path, but I am also expecting that wildcards could be used. With that said, I know I can easily use MD C:\SomePath\ or New-Item "C:\SomePath\" -FileType Directory IF the path provided is absolute; however, it fails whenever I try to do this with wild cards.
Examples: These all fail when I attempt them
MD "C:\*\MyBackups\AppBackup"
New-Item "C:\*\MyApp\Backup" -FileType Directory
$fullPath = "C:\*\MyApp\Backup" | Resolve-Path
$fullPath = Resolve-Path "C:\*\MyApp\Backup"
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("C:\*\MyApp\Backup")
Now, I understand part of the failure is the wildcard itself as the command doesn't understand how to interpret it.
I have researched things like New-Item, Convert-Path, Resolve-Path, and Split-Path, but haven't been able to find anything that is related to what I'm trying to do.
md, which is an alias for a wrapper function around New-Item, expects an exact path. If you want to create a directory based on a wildcard path you need something like this:
Get-ChildItem 'C:\*\MyBackups' | ForEach-Object {
New-Item -Type Directory -Path $_.FullName -Name 'MyBackup' | Out-Null
}
Note that wildcards in a path usually cover just one level of hierarchy. If you want to find any subfolder MyBackups on the C: drive you need an approach like this:
Get-ChildItem 'C:\' -Filter 'MyBackups' -Directory -Recurse | ForEach-Object {
New-Item -Type Directory -Path $_.FullName -Name 'MyBackup' | Out-Null
}
If you'r attempting to take user input and make that represent the *, then you need to represent that in your code. The only area that confuses me is that you have wildcards. I don't think I fully understand what purpose the wildcards serve in your script.
$backupTemplate = 'MyBackups\AppBackup'
#This can be a lot of different methods of input, from a Read-Host or what I have below
$dirSelector = New-Object System.Windows.Forms.FolderBrowserDialog -Property #{ SelectedPath = Get-Location }
$dirSelector.ShowDialog() | Out-Null
$dirSelector.SelectedPath
$bckupDir = "$($dirSelector.SelectedPath)\$backupTemplate"
#EDIT: You were indicating below you needed to find the parent directory for the 'anchor'. Added this here for that.
$parentDir = (Get-Item $dirSelector.SelectedPath).Parent.FullName
if(-not (Test-Path -Path $bckupDir))
{
New-Item -Path $bckupDir
}
#Execute your code here on whatever is doing the backup.
EDIT/Addition:
If the problem you are running in to is that the User sets their backup directory once and then may not remember where it was, then you should look into storing this variable somewhere that both the user can recall in your cmdlets and your cmdlets can access to utilize.
A very common way to do this is to create a file that holds the settings that the user makes in regards to whatever you are attempting to accomplish.
A nice way to do this is to create a standard object and then export that as an CliXML (or JSON or whatever format you like to work with) like so:
$userSettings = New-Object -TypeName psobject
$userSettings | Add-Member -MemberType NoteProperty -Name User -Value $env:USERNAME
$userSettings | Add-Member -MemberType NoteProperty -Name LastUpdate -Value $(Get-Date -format u)
$userSettings | Add-Member -MemberType NoteProperty -Name BackupDirectory -Value $dirSelector.SelectedPath
$userSettings | Export-Clixml -Path "$env:USERPROFILE\psUserProf.xml"
#when you need to get the users backup folder in the future you just import the profile and work from there
$userSettings = Import-Clixml -Path "$env:USERPROFILE\psUserProf.xml"
$userSettings.BackupDirectory
Note though that if your users have more than one computer you'll need to facilitate a way for these settings to follow them.

Setting an alias with attributes in PowerShell

I wanted to set an alias for listing files in the directory, but Set-Alias -name lf -value ls -file does not seem to work. I intend to use this the Unix alias way.
An alias can't do that. From the help for Set-Alias:
You can create an alias for a cmdlet, but you cannot create an alias for a command that consists of a cmdlet and its parameters.
However, using a technique called "splatting", a function can do it easily:
function lf {
ls -file #args
}
For more information, see help about_splatting.
Example 5 from Get-Help Set-Alias -Full is what you want:
Function lsfile {Get-Childitem -file}
Set-Alias lf lsfile
Append to the answer from #mike-z .
You can put the function definition into the PowerShell profile so that you can reuse it opening shell again.
test-path $profile
// Ensure it doesn't exists before creating the profile!
new-item -path $profile -itemtype file -force
notepad $profile
Simply put the code into the file:
function lf { ls -file #args }
You can check the details from official documentation.