Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Oct 8, 2025

Summary

Implements a new Reset-PSResourceRepository cmdlet that allows users to reset the repository store (PSRepositories.xml) to its default state with only PSGallery registered. This addresses the issue where users have no way to recover when the repository store becomes corrupted.

Fixes #1839

Problem

When the PSResourceRepository.xml file becomes corrupted (e.g., through manual editing, file system errors, or incomplete writes), PSResourceGet cmdlets fail with XML parsing errors. Users were left with no built-in recovery mechanism and had to manually locate and delete the file, which is not user-friendly.

Solution

This PR introduces:

1. New Reset-PSResourceRepository Cmdlet

A new cmdlet that safely resets the repository store to its default configuration:

# Basic usage with confirmation prompt
Reset-PSResourceRepository

# Test what would happen
Reset-PSResourceRepository -WhatIf

# Return PSGallery info after reset
$result = Reset-PSResourceRepository -PassThru

Key Features:

  • ShouldProcess support with ConfirmImpact=High to ensure users understand this is a significant action
  • -PassThru parameter to return the PSGallery repository information
  • Verbose output showing the file path being reset
  • -WhatIf and -Confirm support for safe testing

2. Safe Reset Implementation

The RepositorySettings.Reset() method implements a safety-first approach as requested:

  1. Creates new XML file in a temporary location first
  2. Validates the new file can be loaded
  3. Backs up the existing file before deletion
  4. Only replaces the old file after the new file is successfully created
  5. Restores the backup if any step fails
  6. Cleans up temporary files in all scenarios

This ensures that if anything goes wrong during the reset process, the original file is preserved and restored.

3. Improved Error Messages

Updated the error message in CheckRepositoryStore() to guide users:

Before:

Repository store may be corrupted, file reading failed with error: XML parsing error.

After:

Repository store may be corrupted, file reading failed with error: XML parsing error. 
Try running 'Reset-PSResourceRepository' to reset the repository store.

Testing

Added comprehensive test coverage with 6 test cases:

  • Basic reset without PassThru parameter
  • Reset with PassThru returning PSGallery
  • -WhatIf support verification
  • Recovery from corrupted file
  • Handling when file doesn't exist
  • Resetting when multiple repositories are registered

Breaking Changes

None. This is a new cmdlet that doesn't modify any existing functionality.

Example Usage

# User encounters corrupted store error
Get-PSResourceRepository
# Error: Repository store may be corrupted, file reading failed with error: Data at the root level is invalid.
# Try running 'Reset-PSResourceRepository' to reset the repository store.

# Reset the store (will prompt for confirmation due to High confirm impact)
Reset-PSResourceRepository

# Verify PSGallery is registered
Get-PSResourceRepository
# Name       Uri                                        Trusted Priority
# ----       ---                                        ------- --------
# PSGallery  https://www.powershellgallery.com/api/v2  False   50

Related Issues

Addresses the concerns raised in issue comments:

  • @StevenBucher98's requirement for ShouldProcess logic to show file location ✅
  • @alerickson's note about documentation for reset scenarios ✅

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1k9vsblobprodcus379.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • 37cvsblobprodcus359.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • 4myvsblobprodcus32.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • 51yvsblobprodcus36.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • 5dkvsblobprodcus355.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • 7devsblobprodcus323.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • 7k6vsblobprodcus337.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • bcnvsblobprodcus378.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • c50vsblobprodcus330.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • dlbvsblobprodcus316.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • fdpvsblobprodcus345.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • gbsvsblobprodcus365.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • kh4vsblobprodcus325.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • kmuvsblobprodcus389.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • m6xvsblobprodcus342.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • pkvvsblobprodcus321.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • s4uvsblobprodcus326.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • tphvsblobprodcus375.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)
  • yttvsblobprodcus357.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet publish --configuration Debug --framework net472 --output bin/Debug/net472/publish (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Repository store (PSRepositories.xml) should have 'reset' functionality</issue_title>
<issue_description>### Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues.

Steps to reproduce

If the repository store gets corrupted, PSGet throws an error from attempting to parse the file. At the very least, error messaging should provide user with steps to remedy the situation. We should, however, also add either a new parameter or a new cmdlet that allows users to delete the old PSRepositories.xml file and create a new default file.

I'm proposing:
Register-PSResourceRepository -Reset
which, again, would delete the current PSRepositories.xml file, create a new one, and then register only the PowerShellGallery.

Expected behavior

If the xml file is unable to be parsed, an error throws telling the user that there was a parse error and suggesting to either try rerunning the command or running Register-PSResourceRepository -Reset to reset the repository store.

Actual behavior

Throws parse error and fails.

Error details

No response

Environment data

All versions and OS.

Visuals

No response</issue_description>

<agent_instructions>create a new cmdlet 'Reset-PSResourceRepository' that will create a new PSRepository.xml file. However, make sure that a new file is created first and once that new file is successfully created the old one is deleted. If the new file cannot be created, restore the old file. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

@StevenBucher98 Adding some ShouldProcess logic in order to make sure the user understands what is happening and where the file is that they are going to reset @alerickson @mgreenegit consider documentation on how to handle a 'reset' scenario
Fixes #611

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Add reset functionality for PSRepositories.xml file Add Reset-PSResourceRepository cmdlet to recover from corrupted repository store Oct 8, 2025
@Copilot Copilot AI requested a review from alerickson October 8, 2025 20:24
Copilot finished work on behalf of alerickson October 8, 2025 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Repository store (PSRepositories.xml) should have 'reset' functionality
2 participants