Make PowerShell suggests current folder content/items using the Tab key - powershell

I'm new to PowerShell,
I'd like to navigate through the file system by getting the current folder content suggestions instead of cmdlets by clicking the Tab key just like in the CMD.
I don't want to use the dir cmdlet, I want to walk through the different items by clicking on the Tab key.
Is that possible and how to do it ?

If you type ex. a<tab>, you will always get file/folders starting with a before it shows any cmdlets and commands in PATH-locations.
If you only want files and folders, type .\<tab>

Related

Task Scheduler | I am trying to schedule a .ps1 file but every time the scheduler runs a pop window appear "How Do you want to open this file?"

Here's a screenshot from scheduled task action settings
First of all make sure the path used for powershell is correct (best option in my opinion is to click Browse... and select the program with the dialog window instead of manually inserting the full path, if that's what you did).
In second place, change Add arguments (optional): to -File "D:\TM1 Model\Test GIIS\...\yourfilename.ps1", please note you should use double quote since the full path contains spaces; again, make sure the full file path is typed correctly.
There should be no need to insert the full path in the in the Program/script box. Most people don't even know where it is.
For all the PowerShell scripts that I run on servers I only type powershll.exe and it works.
I agree with Giacomo (Calvin?), the value in the Add Arguements box needs to be in quotes if you have spaces in the folder or file name. ie -File "C:\Batch Files\checkdisk.bat"
The -File argument is telling PowerShell the file you want powershell.exe to run.

Change filter approach in powershell tab auto complete

Currently in Powershell when I use tab to check the possible options for completion it show just items that has the param text in the beginning of the word.
Example: Given the items
Program Files
Local Programs
Another item
When
cd Pro [tab]
Returns:
Program Files
So, it will show as option all the directories where the name starts with Pro. But I want to change this behavior, instead of the premise "Starting with" I would like a "Contains".
In this case my outcome would be:
Example: Given the items
Program Files
Local Programs
Another item
When
cd Pro [tab]
Returns:
Program Files
Local Programs
I don't know if there is another way, I would say so, but I solved it with TabExpansion:
function TabExpansion($line, $lastWord) {
if ($line.StartsWith("cd ")) {
return Get-ChildItem -Name *$lastWord*
}
}

Passing the current File Path through to Powershell "SendTo" Script

I'm trying to create a script that will let me copy an item from one location to a specified location in a PowerShell script. Before it's mentioned, I know that I can put a shortcut in the Send To directory to perform this action, but I would like to add additional features (that's for a different time). I do apologize if there is another post relating to this, I've been looking for a day and a half to find one.
What I would like to know is if there is a way I can pass the current-item-that-I-am-right-clicking's file path to PowerShell to be used with the Copy-Item function. So if there is an item on the desktop, I can use this PowerShell script to Copy-Item C:\Users\USERNAME\Desktop\File.ext using the path as a variable (I believe that would be the appropriate term) from the "Send To" Selection in the context menu.
You don't have to do anything special to get the Send To context menu to send the file path to the target - it happens automatically.
Try the following:
Create a new script, let's call it C:\Copy-SendTo.ps1
param($SourceFile)
Copy-Item $SourceFile -Destination C:\your\specific\location\
Now, create a shortcut in $env:APPDATA\Microsoft\Windows\SendTo with the name "CopyTo" and the following target:
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe C:\Copy-SendTo.ps1 -SourceFile
Now right-click a file on your desktop, select Send To > CopyTo.
Voila, your file has been copied

How to open a file without typing its entire name?

I want to be more comfortable with CLI, so I need practice. :D
I have a directory and within it I have more of them. I use ls to find their name, but the one I want to change into has a very long name.
Is it possible to change to that directory without typing his entire name?
Try to type few chars of that name and press Tab once to complete that name.
If it doesn't complete, press Tab twice to see available names that start with the character sequence that you entered. Then, add few more chars, so that sequence is unique for that name and press Tab to complete.

Is it possible to change the default value of $profile to a new value?

So I would rather not create my profile file here:
C:\Users\fmerrow\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
I mean don't get me wrong, this isn't the end of the world and I can live with it. However, I like to keep root "My Documents" reasonably lean and I really would rather not create a directory there every time I start using a new application.
I've nosed around looking to where this setting might be hidden, but so far no luck. It doesn't seem to be in the registry or any of the $PsHome files.
Do I just have to learn to live with this? . . . or is there a way to change the value of $profile that will "stick" on this system for all time? That is, to change the "default value" of $profile?
The best I've thought of so far, is to ignore $profile and instead put some code in $profile.AllUsersAllHosts to source/execute my file from where I want to put it instead of from the default $profile location.
Comments and/or other suggestions welcomed.
Frank
The only thing I can think of is "dot sourcing" your profile at the powershell invocation.
For example:
powershell -noprofile -noexit -command "invoke-expression '. ''C:\My profile location\profile.ps1''' "
By changing the script that invoke-expression command points to you can place your "profile" anywhere you'd like. Then, create shortcut that launches PowerShell and set the target to the above command.
You can change your $Profile.CurrentUser* paths by changing your personal folder path Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Either via regedit
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
Under the Name column select Personal and chage the value to where you want your profile.
Or via PowerShell
New-ItemProperty
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
Personal -Value 'Your New Path Here' -Type ExpandString -Force
You have to reboot for this to take effect.
You can also put your profile file here
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
If you want to have a separated location for all your profiles and scripts, you can modify your profile.ps1 file above as
$profile = "NewLocation\profile.ps1"
. $profile
Make sure what type of profile you use, see details here
https://technet.microsoft.com/en-ca/library/dd819434.aspx
Try junctions by running this command in powershell:
cmd /c mklink /J c:\Users\Name\Documents\WindowsPowerShell\ d:\Powershell\Engine\Profile\
For more information about junctions see here.
I think your solution to source your "new" profile in the existing profile is probably as good as you're going to get.
This solution is inspired by RootLoop's answer:
Access your profile by navigating to its location defined by $PROFILE. (For me, that location happened to be C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1. Then, go ahead and move the contents of your customized profile to wherever you want it to be, (C:/NewLocation/profile.ps1, let's suppose). Replace the original profile's contents (the file C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1) with the text:
$profile = "C:\NewLocation\profile.ps1"
. $profile
Remember that the profile is just a script that is run as soon as you open powershell. This script above will first set $profile to the new location, so any references to the $profile variable will still work as if you moved it. The next line of code will invoke the new profile with syntax that is called dot sourcing. Effectively, the . $profile line is just running your new profile code.
Before that will work on your system, you may have to loosen your execution policy. See https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts for details on that.
Next, you can reduce the clutter in your My Documents directory by hiding the Powershell folder. Simply right click on the folder, select "properties", and under the general tab, select "hidden". And voila! - You have effectively created the illusion that you moved your profile location, without having to do much tinkering with system settings!
According to Scripting Guy article Understanding the Six PowerShell Profiles, $profile is expanded from $PsHome\Microsoft.PowerShell_profile.ps1; $pshome is the powershell installation directory and a read-only variable; according to a post on this thread, Microsoft tells us this cannot be changed.
This might be more of a workaround, but what I did was create a symbolic link copy of the WindowsPowerShell directory in the location PowerShell was looking at. This is more of a bandaid technique though.