Open program files directory using powershell and env variable - powershell

My goal is to create a PowerShell script, which would open specific directories in Windows Explorer.
Some of the directories could be referenced through the environment-variables.
However I got a problem with the following command
ii %programfiles(x86)%
Execution returns the following error:
The term 'x86\' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:23
+ ii %programfiles\(x86\ <<<< )%
+ CategoryInfo : ObjectNotFound: (x86\:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Can you kindly explain to me, what I'm doing incorrectly here?

%variable% is batch notation. In PowerShell you have to use $env: to access environment variables.
Invoke-Item ${env:ProgramFiles(x86)}
The curly brackets are required, because without them the parentheses wouldn't be recognized as part of the variable name.

Related

PowerShell The term 'which' is not recognized as cmdlet function script file or operable program

I am using PowerShell and want to do a which command, however I am seeing this error:
which : The term 'which' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ which xxxxxx
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (which:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have done a search on Google and there is no specific answer to my question. Has anyone encountered the same error before and how can I fix it?
Putting an answer together from the comments:
which is a standard command of Linux, but is not normally present on Windows.
The equivalent command is Get-Command, or if you have it, you can run where.exe.

Running a PowerShell Script .PS1

I am trying to generate a MachineKey for my application using the PowerShell script found in kb2915218.
I have copied the function into notepad and saved as a .PS1 file. Now if I look at this file through explorer it is being recognised as a PowerShell file.
I then have run PowerShell and CD to the directory of my .PS1 file.
I then ran the following command:
Set-ExecutionPolicy Unrestricted
followed by:
.\Powershell-Generate-MachineKey.ps1
(the name of my script). And finally I then tried running the command
Generate-MachineKey
However I get the message:
Generate-MachineKey : The term 'Generate-MachineKey' is not recognized as the
name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Generate-MachineKey
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Generate-MachineKey:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Can someone please tell me where I am going wrong here?
The script just defines a function, so if you execute it like this:
.\Powershell-Generate-MachineKey.ps1
it won't do anything, because the function isn't invoked anywhere and also isn't made available in the current context. For the latter you need to dot-source the script
. .\Powershell-Generate-MachineKey.ps1
The dot-operator basically executes the script in the current context instead of a child context. That way the definitions from the script become available in the current context, and you can invoke the function like this:
Generate-MachineKey

Windows PowerShell cannot recognize sqlite3

I have installed SQLite3 on my computer in G:\SQLite3\sqlite3.exe
However, when I type "sqlite3" (no quotes) to PowerShell, it gives me the following error:
sqlite3 : The term 'sqlite3' is not recognized as the name of a cmdlet, functio
n, script file, or operable program. Check the spelling of the name, or if a pa
th was included, verify that the path is correct and try again.
At line:1 char:1
+ sqlite3
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (sqlite3:String) [], CommandNotF
oundException
+ FullyQualifiedErrorId : CommandNotFoundException
My Environmental Path includes G:\SQLite3, so when I run sqlite3 in command prompt (cmd.exe), it runs just fine. I prefer PowerShell though, so I would be glad, if someone could point me in the right direction how to make it accept this command. If that matters, I use Windows 8.
Most likely the directory G:\SQLite3 is not in your PATH environment variable, so PowerShell doesn't know where to look for the executable. Either run the executable with its full path, or add the directory to the $env:PATH:
$env:PATH += ';G:\SQLite3'
& sqlite3.exe

How do I get "hello world" script to run from the command line in powershell?

I can not get pass this error message.
Thanks
The term 'power-date.ps1' is not recognized as the name of a cmdlet, function, script file
, or operable program. Check the spelling of the name, or if a path was included, verify t
hat the path is correct and try again.
At line:1 char:15
+ power-date.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (power-date.ps1:String) [], CommandNotFound
Exception
+ FullyQualifiedErrorId : CommandNotFoundException
PowerShell will not run scripts in the current directory unless you prefix them with .\ e.g.:
.\power-date.ps1
This is a security feature that prevents someone from dropping a script called dir or ls into a directory the hacker can access and that the user is often in. The hacker's intention being that when the user executes dir, the fake dir.ps1 wwould be invoked unknownst to the user. This security feature prevents this sort of attack. If the user wants to invoke the script dir.ps1, they have to explicitly reference it by path e.g. .\dir.ps1

Running a function in PowerShell

I am new to PS and have been given a script to run. The first thing I do is type in c:\powershell\ir.ps1. This seems to work. Then after defining my clients directory I am supposed to be able to just type in functions such as ir-n. This worked at the person's desk that showed me how to do it, but I get the following error:
The term 'ir-n' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:1 char:5
+ ir-n <<<<
+ CategoryInfo : ObjectNotFound: (ir-n:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Is there something simple I can do to get it to run? I see the function in the ir.ps1 file so I know it is there.
It looks like you are running the ir.ps1 script when you should be sourcing it. I'm guessing that the ir.ps1 file is defining a function named ir-n. In that case running the script will not define the function in the script's context but not the command window. You need to source the script to get it to persist in the command window.
Try running the following
PS$> . c:\powershell\ir.ps1
After running this then try ir-n.
You probably need to dot source the script which will leave the functions it defines available in the global scope e.g.:
PS> . c:\powershell\ir.ps1