Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Management.Automation;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// The implementation of the "Remove-Alias" cmdlet.
/// </summary>
///
[Cmdlet(VerbsCommon.Remove, "Alias", DefaultParameterSetName = "Default", HelpUri = "")]
[Alias("ral")]
public class RemoveAliasCommand : PSCmdlet
{
#region Parameters

/// <summary>
/// The alias name to remove.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string[] Name { get; set; }

/// <summary>
/// The scope parameter for the command determines
/// which scope the alias is removed from.
/// </summary>
[Parameter]
public string Scope { get; set; }

/// <summary>
/// If set to true and an existing alias of the same name exists
/// and is ReadOnly, it will still be deleted.
/// </summary>
[Parameter]
public SwitchParameter Force { get; set; }

#endregion Parameters

#region Command code

/// <summary>
/// The main processing loop of the command.
/// </summary>
protected override void ProcessRecord()
{
foreach(string aliasName in Name)
{
AliasInfo existingAlias = null;
if (String.IsNullOrEmpty(Scope))
{
existingAlias = SessionState.Internal.GetAlias(aliasName);
}
else
{
existingAlias = SessionState.Internal.GetAliasAtScope(aliasName, Scope);
}

if (existingAlias != null)
{
SessionState.Internal.RemoveAlias(aliasName, Force);
}
else
{
ItemNotFoundException notAliasFound = new ItemNotFoundException(StringUtil.Format(AliasCommandStrings.NoAliasFound, "name", aliasName));
ErrorRecord error = new ErrorRecord(notAliasFound, "ItemNotFoundException",ErrorCategory.ObjectNotFound, aliasName);
WriteError(error);
}
}
}
#endregion Command code
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
"Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
"Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash",
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb", "Format-Hex"
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb", "Format-Hex", "Remove-Alias"
FunctionsToExport= "Import-PowerShellDataFile"
AliasesToExport= "fhx"
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
"Get-PSBreakpoint", "Remove-PSBreakpoint", "New-TemporaryFile", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
"Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash",
"Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb", "Format-Hex"
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb", "Format-Hex", "Remove-Alias"
FunctionsToExport= "ConvertFrom-SddlString"
AliasesToExport= "fhx"
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Describe "Remove-Alias" -Tags "CI" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one of the things that could happen on dev boxes is that an alias tral could exist which could cause some test failures. Since it doesn't matter, should this be something more unlikely to have a collision (say a generated guid)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PowershellNinja Could you please address the request?
Sample:

Describe "Remove-Alias" -Tags "CI" { 
    BeforeAll {
        $testAliasName = (New-Guid).Guid
    }
...


BeforeAll {
$testAliasName = (New-Guid).Guid
}

It "Remove-Alias should remove a non-readonly alias"{
{
Set-Alias -Name $testAliasName -Value "Remove-Alias" -ErrorAction Stop
Remove-Alias -Name $testAliasName -ErrorAction Stop
Get-Alias -Name $testAliasName -ErrorAction Stop
} | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
}

It "Remove-Alias should throw on a readonly alias"{
{
Set-Alias -Name $testAliasName -Value "Remove-Alias" -Option ReadOnly -ErrorAction Stop
Remove-Alias -Name $testAliasName -ErrorAction Stop
} | ShouldBeErrorId 'AliasNotRemovable,Microsoft.PowerShell.Commands.RemoveAliasCommand'
}

It "Remove-Alias should remove a non-readonly alias with force"{
{
Set-Alias -Name $testAliasName -Value "Remove-Alias" -ErrorAction Stop
Remove-Alias -Name $testAliasName -Force -ErrorAction Stop
Get-Alias -Name $testAliasName -ErrorAction Stop
} | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
}

It "Remove-Alias should remove a readonly alias with force"{
{
Set-Alias -Name $testAliasName -Value "Remove-Alias" -Option ReadOnly -ErrorAction Stop
Remove-Alias -Name $testAliasName -Force -ErrorAction Stop
Get-Alias -Name $testAliasName -ErrorAction Stop
} | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
}

It "Remove-Alias should throw if alias does not exist"{
{
Get-Alias -Name $testAliasName -ErrorAction SilentlyContinue | Should BeNullorEmpty
Remove-Alias -Name $testAliasName -ErrorAction Stop
} | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.RemoveAliasCommand'
}

It "Remove-Alias should remove multiple alias at once"{
{
Set-Alias -Name "$testAliasName" -Value "Remove-Alias" -ErrorAction Stop
Set-Alias -Name "$testAliasName-2" -Value "Remove-Alias" -ErrorAction Stop
Set-Alias -Name "$testAliasName-3" -Value "Remove-Alias" -ErrorAction Stop
Remove-Alias -Name "$testAliasName","$testAliasName-2","$testAliasName-3" -ErrorAction Stop
Get-Alias -Name "$testAliasName" -ErrorAction Stop | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
Get-Alias -Name "$testAliasName-2" -ErrorAction Stop | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
Get-Alias -Name "$testAliasName-3" -ErrorAction Stop | ShouldBeErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'
}
}

It "Remove-Alias should throw on out-of-range scope"{
{
Set-Alias -Name $testAliasName -Value "Remove-Alias" -ErrorAction Stop
Remove-Alias -Name $testAliasName -Scope 99999 -ErrorAction Stop
} | ShouldBeErrorId "ArgumentOutOfRange,Microsoft.PowerShell.Commands.RemoveAliasCommand"
}
}
1 change: 1 addition & 0 deletions test/powershell/engine/Basic/DefaultCommands.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Register-ObjectEvent", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Register-PSSessionConfiguration", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Register-WmiEvent", , $($FullCLR )
"Cmdlet", "Remove-Alias", , $( $CoreWindows -or $CoreUnix)
"Cmdlet", "Remove-Computer", , $($FullCLR )
"Cmdlet", "Remove-Event", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Remove-EventLog", , $($FullCLR )
Expand Down