見出し画像

Windows11でpowershell.exeが存在しないという問題

PS C:\Windows\System32 (10:30:12) >  powershell.exe -command 'Get-Clipboard'
powershell.exe: The term 'powershell.exe' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
PS C:\Windows\System32 (10:33:10) >  powershell.exe -command 'Get-Clipboard'

PS C:\Windows\System32 (10:43:02) >  powershell.exe -command 'Get-Clipboard'
午後10:10 · 20231115日
·
7,237
 件の表示
PS C:\Windows\System32 (10:43:16) >
[~] ndel.py                                                                                                                        10:20:20
Traceback (most recent call last):
  File "/home/a66/hbin/ndel.py", line 6, in <module>
    src = pyperclip.paste() # クリップボードからテキスト読み込み
  File "/home/a66/.local/lib/python3.10/site-packages/pyperclip/__init__.py", line 681, in lazy_load_stub_paste
    return paste()
  File "/home/a66/.local/lib/python3.10/site-packages/pyperclip/__init__.py", line 511, in paste_wsl
    p = subprocess.Popen(['powershell.exe', '-command', 'Get-Clipboard'],
  File "/usr/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1863, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'powershell.exe'
[~] cat hbin/ndel.py                                                                                                               10:20:30
#!/usr/bin/python3

import pyperclip

# 例えば"一行目\n二行目"をコピーしているとする
src = pyperclip.paste() # クリップボードからテキスト読み込み
res = src.replace("\n","") # 改行削除
pyperclip.copy(res) # クリップボードにテキストを戻す

print(src)

[~] powershell.exe -command 'Get-Clipboard'                                                                                        10:22:24
zsh: command not found: powershell.exe
[~] pwsh.exe -command 'Get-Clipboard'                                                                                              10:24:51
pwsh.exe -command 'Get-Clipboard'
[~] which pwsh.exe                                                                                                                 10:37:28
/mnt/c/Program Files/PowerShell/7/pwsh.exe
[~] cd /mnt/c/Program\ Files/PowerShell/7/                                                                                         10:39:59
[7] cp pwsh.exe powershell.exe                                                                                                     10:40:46
cp: 通常ファイル 'powershell.exe' を作成できません: 許可がありません
[7] sudo cp pwsh.exe powershell.exe                                                                                                10:41:25
[sudo] a66 のパスワード:
cp: 通常ファイル 'powershell.exe' を作成できません: 許可がありません
[7] explorer.exe .                                                                                                                 10:41:39
[7] ndel.py                                                                                                                        10:41:58
Traceback (most recent call last):
  File "/home/a66/hbin/ndel.py", line 6, in <module>
    src = pyperclip.paste() # クリップボードからテキスト読み込み
  File "/home/a66/.local/lib/python3.10/site-packages/pyperclip/__init__.py", line 681, in lazy_load_stub_paste
    return paste()
  File "/home/a66/.local/lib/python3.10/site-packages/pyperclip/__init__.py", line 517, in paste_wsl
    return stdout[:-2].decode(ENCODING)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 473: invalid start byte
[7]   

 「C:\Program Files\PowerShell\7」というフォルダに移動し、pwsh.exeをコピーしてpowershell.exeを作成(管理者権限)で一応解決。しかし、日本語が含まれると文字コードの問題が発生しいろいろやってみたが解決できず。

 WSLの環境で使っているpyperclipというpythonのライブラリーがPowershellを使っていたとは意外な発見。

 WSLからマウントで読み出しているらしい。

[~] which pwsh.exe                                                                                                                 11:57:59
/mnt/c/Program Files/PowerShell/7/pwsh.exe
[~] which powershell.exe                                                                                                           12:04:15
/mnt/c/Program Files/PowerShell/7/powershell.exe
[~]  

この記事が気に入ったらサポートをしてみませんか?