When I Run PowerShell script always changes back to User Directory - powershell

Strange issue I've not seen. Started working somewhere new so not sure if it's a quirk but each time I run a PS Script the first thing it seems to do is change back to my user DIR (c:\Users\UserName)
seems to do this at the start of the script because if I output any files that's where they end up (obvs unless I use explicit path)
Bit annoying I'm gonna try work out why it's doing it but thought I'll ask a bunch of smarter people.
Cheers.

Thanks for the quick reply guys I dind't notice when connecting to Exchange Online with MFA it goes to that the userprofile folder.
$CreateEXOPSSession = (Get-ChildItem -Path $env:userprofile -Filter
CreateExoPSSession.ps1 -Recurse -ErrorAction SilentlyContinue -Force | Select -Last
1).DirectoryName
. "$CreateEXOPSSession\CreateExoPSSession.ps1" Connect-EXOPSSession -UserPrincipalName myUPN
It's early morning here I need a coffee!!
thanks again

Related

How to use Powershell Invoke-Item with a gci search?

I'm trying to build a Powershell script that uses the Invoke-Item cmdlet once I've found the files using the following script:
gci -Recurse -File *.xls* C:\folder_example\'  |  sort LastWriteTime | Select -last 2 | 
I'm not sure how best to implement the Invoke-Item cmdlet from here?
In its simplest usage, Invoke-Item (aliased as ii) just needs a path given to it either through pipeline or through a parameter.
In this case, it could be as simple as adding | Invoke-Item to the end of your command:
gci -Recurse -File *.xls* C:\folder_example\ | sort LastWriteTime | select -Last 2 | Invoke-Item
One thing I will mention, is that I have found that certain programs cannot handle a bunch of files thrown at them in quick succession like this script will do.
I usually run into this when trying to open a bunch of files in Notepad++.
I'm going to simplify the command a bit just to keep it short...but usually one of these two changes will fix my problem:
Get-ChildItem -Filter *.xls* | ForEach-Object { Invoke-Item $_.FullName }
Placing the call into a ForEach-Object script block will sometimes fix this. I'm not 100% why this fixes it, I have some theories but that's all they are, so I will keep them out of this answer. But it always fixes the issue for me when using Invoke-Item on Notepad++ files.
Some programs, like Excel, have a start up splash screen and some lag before they eventually open. So you could always throw a sleep in there just to help slow things down a bit in case you are still having trouble:
Get-ChildItem -Filter *.xls* | ForEach-Object { Start-Sleep 0.1; Invoke-Item $_.FullName }
That will add a small 100ms pause just to give whatever application you are running to have a little time to process.

Could you help me with a script (made on Powershell) to delete files (eventualy subfolders) of a specific folder that are older than 5 days?

As I said in the question, I have tried to create a script that would delete files and subfolders of a folder that are older than 5 days.
I am new to Powershell, I have already seen some tutorials and read about the syntax of it, but that has not helped me much, so I just found a code that is alike the one I need and I have tried to adapt it to my needs. I will paste it here:
$limit = (Get-Date).AddDays(-5)
$path = "C:\Users\Me\Desktop\example"
Get-ChildItem -Path $path -Recurse | Where-Object {$_.PSIsContainer -and
$_.CreationTime -lt $limit} | Remove-Item
The script has runned, without any error message, but the files have not been deleted. I would like to know what may be the problem. By the way, would this script delete subfolders as well? Other thing, how do I do to schedule this script?
Note: As I said, I am a beginner, so if someone could clarify these objects for me, I would be very glad: "PSIsContainer", "CreationTime", "|". I have already search what this "$_." means, but I have not understood it well yet, so if someone could tell me it directly, I would be very thankful as well. Thank you in advance.
I have tried a different code structure, but that basically does the same thing, and it has worked:
$Now=Get-Date
Get-Childitem C:\Users\Me\Downloads | Where-Object { $_.LastWriteTime –lt
$Now.AddDays(-5) } | Remove-Item
Now I am just not sure if it will delete subfolders as well, and I still need to schedule it, but I will keep on trying it and if I get it, I will bring some update about it here.

Finding Recently Downloaded Files in Power Shell

I'm a blind user and I keep having to go find what's breaking a few people's computers and it's getting annoying. They always click on "install this Active-X" or "download the free video player now" and I have to then dig through everything.
I whipped up a powershell script to search C:\ for files that have a write time of 5 minutes ago and less for testing purposes the Get-ChildItem part works. Now I just want to get a list of file paths to make my life easier but I am missing something.
Here's what I have so far:
cd c:\
$fileizer = Get-ChildItem -Path . -exclude *.txt,*.log -ErrorAction SilentlyContinue -Recurse| ? {$_.LastWriteTime -gt (Get-Date).AddMinutes(-5)}
echo $fileizer
Here are the results if I just do the Get-ChildItem part of it:
PS C:\Users\tryso> c:\bin\hours.ps1
Directory: C:\bin
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/1/2017 2:44 PM 169 hours.ps1
PS C:\>
Obviously I am going to narrow down the path to something more specific than just C:\ like to get into C:\Windows\Temp and C:\Users\ and the likes, I'm just wondering how to parse everything to just give me a list of files and their path.
I'd also like to point out that 5 minutes old is dumb, yes I know. I just did that to make it scream through my C:\ drive because you'd be amazed at how many files have a write time of .5 hours in C:\ LoL.
Ultimately I'd like to figure out how to find new files as opposed to recent write times if that's possible.
Sorry if my query is lame or a repeat, the only close examples I have found don't work for me for some reason and I'm pretty new at PS scripting - but it's getting pretty addicting and awesome LoL.
Thanks a million for any help!
Ryan
The Select-Object cmdlet can pull out the information you're looking for. Often you will want to know more than one piece of info on your results, so dot sourcing isn't going to be the most efficient.
Try something like this to see the full path, size and last modified timestamp:
Get-ChildItem -Path $path -exclude .txt,.log -ErrorAction SilentlyContinue -Recurse | Where-Object {$_.LastWriteTime -gt (Get-Date).AddMinutes(-5)} | Select-Object FullName, Length, LastWriteTime

Powershell script required

this is the scenario.
p1
|_f1
|_f2
p2
|_f1
|_f2
Can anyone please help me with a powershell script that copies the files shown above from the TFS to a temporary folder ??? where f1,f2,and so on are the subfolders..
I have no experience with either, but in the interest of a least pointing you in the right direction, check out this site. http://coolthingoftheday.blogspot.com/2009/03/pstfs-powershell-and-tfs-better-than.html
There are a couple of commands that will give you at least part of what you want. You will still need to do some digging to figure out the time stamp stuff.
You may want to check the answer to my question on a very similar scenario.
You will find answer to
give me all files in this folder (or subfolder)
as well as
that where modified after x/y/zzzz
but I'm still not sure about the
dump those files to folder other than they would normally go to
update
Incorporating your approach
Get-TfsItemProperty $/MyFirstTFSProj -r -server xyzc011b |
Where {$_.CheckinDate -gt (Get-Date).AddDays(-30)} |
Copy-Item -Destination C:\SomeDir -Whatif
you normally can omit the Copy-Item -Path param because it will be provided by the pipeline.
I don't have a TFS at to test with Get-TfsItemProperty but you could try
Get-TfsItemProperty $/MyFirstTFSProj -r -server xyzc011b |
Where {$_.CheckinDate -gt (Get-Date).AddDays(-30)} |
Get-Member
do find out about where this $null value is coming from.
I assume you did already see this post. To maintain the folder structure on the destination you need to include the -Force switch on the Copy-Item to create missing target folders:
Get-TfsItemProperty $/MyFirstTFSProj -r -server xyzc011b |
Where {$_.CheckinDate -gt (Get-Date).AddDays(-30)} |
Copy-Item -Destination C:\SomeDir -Force -Whatif
I'm still not sure if you need to retrieve/export the files prior to copy them - you should check on the second answer from Richard Berg in the post mentiond above.

Get powershell to display all paths where a certain file can be found on a drive

I'm trying to build a function that will show me all path's where a certain filename is located. The function would take one parameter, that being the file name.
The result would be either a list of all paths, or a message saying there's no such file on the system.
I'm new to Powershell, and I'm not getting the syntax just yet.
I've tried this:
Get-ChildItem -Path -Include notepad.exe
But that threw an error message. I'm currently trying:
$i="notepad.exe"
Foreach ($i in Get-ChildItem c:\ -Recurse){echo -Path}
Started that now, it's still running, don't know what'll happen, really.
EDIT: echo'd an enormous amount of lines that just say "-Path"...
Can anybody help with this problem? I'm running Powershell 1.0 by the way.
So, to explain what I wish to see when executing this command, here is an example of what I expect after looking for *.txt:
C:/foo.txt
C:/A/foobar.txt
C:/A1/foo.txt
And so on, listing the path to all .txt files on my harddrive. Only the paths, one per line, no extra info needed.
EDIT2:
I've done it. I'm gonna leave this question up for those who make look for this in the future.
The function I used was this(this specific example will hand you a list of all .zip files on your harddrive, edit where needed):
Get-ChildItem -Path c:\ -Include "*.zip" -Recurse -Force -Name > c:\listOfPaths.txt
This created a file called listOfPaths.txt on my C:\ folder and this contained a list of all occurences of any file ending with .zip in all subfolders of my harddrive.
The "c:\" bit isn't mentioned, but I don't mind.
EDIT3:
thanks capar for a more complete version.
Here is capar's code(or how I got it to work, since Get-Children doesn't work in 1.0)
Get-ChildItem -Path c:\ -Recurse *.txt | Select-Object -Property FullName
Since it's Friday night, I decided to play with Power Shell to see if I can help :)
This comes pretty close to what you are asking for I think:
Get-ChildItem -Path c:\ -Recurse *.txt | Select-Object -Property FullName
If it helps, this command will list the properties of any object that will be returned by Get-ChildItem:
Get-ChildItem | Get-Member
ls c:\ -r | ? {$_.name -eq "notepad.exe"}
Get-Children is not recognized in Powershell V3 either. It would be great if someone removed that bad example.
As a warning to anyone searching for files: C:\ on today's hard drives will take a long time to run. You are well advised to narrow your search as much as you can. Since your folder structure might include spaces or special characters, use the typewriter quote (") or apostrophe (') delimeters.
$mylistoffiles = Get-ChildItem -Path 'C:\Windows\Setup\Scripts' -Recurse *.cmd | Select-Object -Property FullName
$mylistoffiles