I tried to store this exact powershell command as a environment variable but problem is either the shell tries to interpret command contents without powershell cognizance or it outputs a wrong command that will not work .
Get this command for me in setx so it works !
<code>powershell -Command "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20"
</code>
<code>powershell -Command "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20"
</code>
powershell -Command "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20"
see what I did
<code> # setx ll20 "powershell -Command "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20""
'Sort-Object' is not recognized as an internal or external command,
operable program or batch file.
HOME@HOME-PC C:UsersHOME
# setx ll20 "powershell -Command "& {Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20}""
SUCCESS: Specified value was saved.
'{Get-ChildItem' is not recognized as an internal or external command,
operable program or batch file.
# echo %ll20%
powershell -Command "
HOME@HOME-PC C:UsersHOME
</code>
<code> # setx ll20 "powershell -Command "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20""
'Sort-Object' is not recognized as an internal or external command,
operable program or batch file.
HOME@HOME-PC C:UsersHOME
# setx ll20 "powershell -Command "& {Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20}""
SUCCESS: Specified value was saved.
'{Get-ChildItem' is not recognized as an internal or external command,
operable program or batch file.
# echo %ll20%
powershell -Command "
HOME@HOME-PC C:UsersHOME
</code>
# setx ll20 "powershell -Command "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20""
'Sort-Object' is not recognized as an internal or external command,
operable program or batch file.
HOME@HOME-PC C:UsersHOME
# setx ll20 "powershell -Command "& {Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20}""
SUCCESS: Specified value was saved.
'{Get-ChildItem' is not recognized as an internal or external command,
operable program or batch file.
# echo %ll20%
powershell -Command "
HOME@HOME-PC C:UsersHOME
I know I can create a ps1 file and invoke it but I want to know if its poss to do this directly
3
You can use doskey for this:
<code>doskey ll20=powershell -c "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20"
</code>
<code>doskey ll20=powershell -c "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20"
</code>
doskey ll20=powershell -c "Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 20"
If you want to make it always available to your shell follow this thread for a couple solutions.