3
I'd like to create some aliases dynamically but I can't get my code to work. Here's the code:
# Drives
$drives = ("a","b","c","d","e")
foreach ($drive in $drives) {
New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.GetNewClosure()
}
function GoToDrive($drive) {
$formatted = "$($drive):\"
if (Test-Path $formatted) {
Set-Location $formatted
} else {
Write-Host "`"$formatted`" does not exist."
}
}
When I type "a", or "b", or any of the letters in $drives, it should change my working directory to that drive's letter (e.g.: A:).
The error I'm getting now is this:
New-Item : A positional parameter cannot be found that accepts argument 'a'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+ New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : A positional parameter cannot be found that accepts argument 'b'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+ New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : A positional parameter cannot be found that accepts argument 'c'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+ New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : A positional parameter cannot be found that accepts argument 'd'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+ New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : A positional parameter cannot be found that accepts argument 'e'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+ New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
Can anybody help me get this to work, please?
Thanks a lot for the clarification and for the working code! I'm accepting yours as the right answer. – prubini87 – 2018-06-10T18:32:48.040