i have this script, but it thorws errors: Exception setting "OutputEncoding":…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
i have this script, but it thorws errors: Exception setting "OutputEncoding": "Nieprawidłowe dojście. " At line:4 char:1
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "InputEncoding": "Nieprawidłowe dojście. " At line:5 char:1
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::InputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8
function Test-StringForEmoji { param ( [string]String ) # This regex targets common Unicode emoji ranges, including those represented by surrogate pairs. # It avoids \U escapes and Unicode properties (\p{}) for broader compatibility with older PowerShell versions. # This should catch most modern emojis without affecting standard punctuation. emojiPattern = '[\u2600-\u27BF\u2B00-\u2BFF\u2300-\u23FF\uFE00-\uFE0F]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|\uD83E[\uDC00-\uDFFF]' return emojiPattern }
$files = Get-ChildItem -Path (Get-Location) -File
$emojiRemovalPattern = '[\u2600-\u27BF\u2B00-\u2BFF\u2300-\u23FF\uFE00-\uFE0F]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|\uD83E[\uDC00-\uDFFF]'
$filesToModify = @()
Write-Host "Scanning for files with emojis in their names..."
foreach (files) { if (Test-StringForEmoji -String file.Name) { # Calculate the new name immediately for display newName = emojiRemovalPattern, ''
# Ensure the new name is not empty or just whitespace after emoji removal
if ([string]::IsNullOrWhiteSpace($newName)) {
Write-Warning "Skipping '$($file.Name)' as removing emojis would result in an empty filename."
continue
}
$filesToModify += [PSCustomObject]@{
OriginalFile = $file
OriginalName = $file.Name
NewName = $newName
}
}
}
if ($filesToModify.Count -eq 0) {
Write-Host "No files with emojis found in their names in the current directory."
} else {
Write-Host "nThe following files contain emojis and will be modified:n"
# Calculate max length for original names for alignment
$maxLength = 0
foreach ($item in $filesToModify) {
if ($item.OriginalName.Length -gt $maxLength) {
$maxLength = $item.OriginalName.Length
}
}
# Add some padding for visual separation
$maxLength += 5 # Increased padding for better separation
Write-Host ("Original Name".PadRight($maxLength) + "--> New Name")
Write-Host ("-".PadRight($maxLength, '-') + "---" + "-".PadRight("--> New Name".Length, '-'))
foreach ($item in $filesToModify) {
# Use direct PadRight and concatenation for robust alignment
Write-Host ($item.OriginalName.PadRight($maxLength) + " --> " + $item.NewName)
}
Write-Host "`nDo you want to proceed with renaming these files? (Y/N)"
$confirmation = Read-Host
if ($confirmation -eq 'Y' -or $confirmation -eq 'y') {
Write-Host "`nRenaming files..."
foreach ($item in $filesToModify) {
$originalFile = $item.OriginalFile
$originalName = $item.OriginalName
$newName = $item.NewName
try {
# Get the file item again using LiteralPath to ensure correct path resolution
$fileToRename = Get-Item -LiteralPath $originalFile.FullName -ErrorAction Stop
Rename-Item -LiteralPath $fileToRename.FullName -NewName $newName -Force -ErrorAction Stop
Write-Host " Renamed '$originalName' to '$newName'"
} catch {
Write-Error "Failed to rename '$originalName': $($_.Exception.Message)"
}
}
Write-Host "`nFile renaming complete."
} else {
Write-Host "`nOperation cancelled by the user."
}
}
The error you're seeing occurs because the script is trying to set console encodings when running in an environment that doesn't have a proper console handle available (like PowerShell ISE, VS Code terminal, or when run as a scheduled task).
Here's the fixed version of your script with proper error handling for the encoding settings:
# Requires PowerShell 5.1 or later for some cmdlets like Get-ChildItem -File
# Set console encoding to UTF-8 for proper Unicode character handling
# Wrap in try-catch to handle environments without console access
try {
if ($Host.Name -eq "ConsoleHost") {
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
}
$OutputEncoding = [System.Text.Encoding]::UTF8
} catch {
Write-Warning "Could not set console encoding. This may affect Unicode character display but won't prevent the script from working."
$OutputEncoding = [System.Text.Encoding]::UTF8
}
# Function to check if a string contains emojis
function Test-StringForEmoji {
param (
[string]$String
)
# This regex targets common Unicode emoji ranges, including those represented by surrogate pairs.
# It avoids \U escapes and Unicode properties (\p{}) for broader compatibility with older PowerShell versions.
# This should catch most modern emojis without affecting standard punctuation.
$emojiPattern = '[\u2600-\u27BF\u2B00-\u2BFF\u2300-\u23FF\uFE00-\uFE0F]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|\uD83E[\uDC00-\uDFFF]'
return $String -match $emojiPattern
}
# Get all files in the current directory
$files = Get-ChildItem -Path (Get-Location) -File
# Define the emoji removal pattern globally so it's consistent
# Using the same precise emoji-targeting pattern for removal
$emojiRemovalPattern = '[\u2600-\u27BF\u2B00-\u2BFF\u2300-\u23FF\uFE00-\uFE0F]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|\uD83E[\uDC00-\uDFFF]'
$filesToModify = @()
Write-Host "Scanning for files with emojis in their names..."
foreach ($file in $files) {
if (Test-StringForEmoji -String $file.Name) {
# Calculate the new name immediately for display
$newName = $file.Name -replace $emojiRemovalPattern, ''
# Ensure the new name is not empty or just whitespace after emoji removal
if ([string]::IsNullOrWhiteSpace($newName)) {
Write-Warning "Skipping '$($file.Name)' as removing emojis would result in an empty filename."
continue
}
$filesToModify += [PSCustomObject]@{
OriginalFile = $file
OriginalName = $file.Name
NewName = $newName
}
}
}
if ($filesToModify.Count -eq 0) {
Write-Host "No files with emojis found in their names in the current directory."
} else {
Write-Host "`nThe following files contain emojis and will be modified:`n"
# Calculate max length for original names for alignment
$maxLength = 0
foreach ($item in $filesToModify) {
if ($item.OriginalName.Length -gt $maxLength) {
$maxLength = $item.OriginalName.Length
}
}
# Add some padding for visual separation
$maxLength += 5 # Increased padding for better separation
Write-Host ("Original Name".PadRight($maxLength) + "--> New Name")
Write-Host ("-".PadRight($maxLength, '-') + "---" + "-".PadRight("--> New Name".Length, '-'))
foreach ($item in $filesToModify) {
# Use direct PadRight and concatenation for robust alignment
Write-Host ($item.OriginalName.PadRight($maxLength) + " --> " + $item.NewName)
}
Write-Host "`nDo you want to proceed with renaming these files? (Y/N)"
$confirmation = Read-Host
if ($confirmation -eq 'Y' -or $confirmation -eq 'y') {
Write-Host "`nRenaming files..."
foreach ($item in $filesToModify) {
$originalFile = $item.OriginalFile
$originalName = $item.OriginalName
$newName = $item.NewName
try {
# Get the file item again using LiteralPath to ensure correct path resolution
$fileToRename = Get-Item -LiteralPath $originalFile.FullName -ErrorAction Stop
Rename-Item -LiteralPath $fileToRename.FullName -NewName $newName -Force -ErrorAction Stop
Write-Host " Renamed '$originalName' to '$newName'"
} catch {
Write-Error "Failed to rename '$originalName': $($_.Exception.Message)"
}
}
Write-Host "`nFile renaming complete."
} else {
Write-Host "`nOperation cancelled by the user."
}
}
The key changes are:
$Host.Name -eq "ConsoleHost" to only set console encodings when running in the actual PowerShell consoleThis should resolve the errors you're seeing while maintaining the script's functionality for removing emojis from filenames.