April 2, 2020 at 8:57 pm #214800
Hi everybody. I am newbie on powershell, i read articles about objects, properties,methods and somehow i understand what they are. I want to understand fully what can i do wit methods and properties. Please explain to me this method: Get-ChildItem | Get-Member gives us method called: “Create” I want to understand how to manipulate with this method on Get-ChildItem. Thanks in advance!
April 3, 2020 at 5:27 am #214893ParticipantTopics: 23Replies: 48Points: 285Rank: Contributor
I don’t know how much experience you have in object oriented programing, but your query reminds me my starting days with Powershell. This is also my first language and I have entered to this programming world by holding hands of Powershell only. Anyway, let’s jump to your query.
Method are actions perform by a object. When you execute get-member, you will get the object type and its members called property, methods etc. Here you mentioned create method. By reading the name, it seems it will create an item. Now how to understand what the method do? Copy the object name, google it, you will find MS document there it will explain all the details very nicely. What a property display, what a method do? What arguments method use, everything. This is another beauty of MS. The documentation.
Another explanation. I don’t know you know about function or not. Methods are defined function of a object. Function takes parameters / arguments to perform something, same applies to methods. If a method needs arguments, those need to pass inside of parenthesis. Ex. Method(arg1,arg2). Now how to know a method needs what type of arguments? Using Powershell, simply call the method without parenthesis. It will display all its constructs. Or else check MS docs as mentioned earlier.
Another quick point. Arguments and parameters are same. Methods use parameters positionally. Whatever value you pass as arguments, it will be supplied position wise. You may not find this in document, but keep it in mind. May be its old programming concept, so it’s common to every programer. I understood the logic by myself with lots of research 😉.
However, Hope I can explain your query. Powershell / Dotnet is well documented. You will get all your answers though those documents. More you read, use more you learn. And finally we are here to help.
Have a nice day.
April 3, 2020 at 7:54 am #214917
Dear Roy, thanks for you reply, explanation and directions how to explore and find informaion.
But i cant find anything about “create” method on get-childitem, so i ask here to help me with this case)
PS C:\> Get-ChildItem | gm
Name MemberType Definition
—- ———- ———-
Create Method void Create(), void Create(System.Security.AccessControl.DirectorySecurity directorySecurity)
April 3, 2020 at 8:42 am #214920ParticipantTopics: 23Replies: 48Points: 285Rank: Contributor
As mentioned, all your answer resides in MSDN.
As per your GM output, it shows using create you can create two object. 1. Directory. 2. Directory Security object.
For directory security, its showing the parameter, what the method is expecting.
Google with System.IO.DirectoryInfo.
Check this url;
So create can create a directory. And DirectorySecurity object which means ACL. If you further click on directory security, you will get related info.
The idea is, you have to navigate within msdn to get all information.
April 3, 2020 at 9:11 am #214926
I understand that all answers reside on MSDN. Before this my first post i have already found what “create” can do. I know that i must read books and pass video courses to understand how to manipulate with powershell. But for this moment i have question, i have problem, i want to know. If you able to explain me how to use “create” method with get-childitem please help.
Get-ChildItem itself is “Get” how it uses method ” Create” to create folders or something else?
Can you create something on powershell and paste here by Get-ChilItem and Create method?
April 3, 2020 at 1:53 pm #214974ParticipantTopics: 12Replies: 523Points: 1,214Rank: Community Hero
I know that i must read books and pass video courses
You’re right. That’s exactly what you need to do. This forum is not designed to provide individual tutoring from scratch. To my knowledge it’s intended to provide mutual help to/from folks who have done some book/video/other PowerShell training.
– Use the PowerShell cmdlets and functions by exploring their parameters, what kind of input they expect, and what kind of output they return.
Stay away from advanced features like using methods because that requires knowledge and understanding of the underlying .Net classes
Master the parameters first. Do all your tasks using the parameters. Only when you have a task that cannot be done with parameters, you will need to look deeper.
For example, to create a file you simply use the Out-File cmdlet as in
'test text 1' | out-file .\myFile1.txt
You can also use other cmdlets depending on the file content, such as export-csv or Export-Clixml
Now to your specific question.
Get-ChildItem | Get-Member gives us method called: “Create”
That’s not entirely accurate.
Get-ChildItem returns objects of type System.IO.FileInfo
Now the System.IO.FileInfo .Net class https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=netframework-4.8 has a .Create method https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo.create?view=netframework-4.8
Example of using this .NET class to create and write to a file may go something like
$File = [System.IO.File]::Create('c:\data\scripts\myfile2.txt') $myTextAsByteArray = [System.Text.Encoding]::UTF8.GetBytes('hello world') $File.Write($myTextAsByteArray,0,$myTextAsByteArray.Count) $File.Close()
You can see the $File type being System.IO.Stream
$File.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False FileStream System.IO.Stream
and the file content
Get-Content .\myfile2.txt hello world
Notice that using .NET methods to create a file and write to it required:
– Instantiating a Stream object as in: $File = [System.IO.File]::Create(‘c:\data\scripts\myfile2.txt’)
– Converting the text to be written to the file to a byte array which is required for the Stream .Create method as in: $myTextAsByteArray = [System.Text.Encoding]::UTF8.GetBytes(‘hello world’)
– Writing the text to the file specifying the start offset and byte count as in: $File.Write($myTextAsByteArray,0,$myTextAsByteArray.Count)
– Closing the file as in: $File.Close()
Not to mention understanding and knowing lots of details about System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, and buffersize.
Not understanding all that can have negative consequences. For example:
will lock all files in the current folder such that you cannot read or write to any of them. This is because under the covers the .Create method of the System.IO.Stream was invoked with the default System.IO.FileShare value of zero which means None. You can only get back to your files by killing the PowerShell.exe or PowerShell_ISE.exe process under which you invoked the ill-advised command (Get-ChildItem).Create() which now has a lock on your files.
Long story short, simply use out-file, stay away from methods and advanced features until you master the standard parameters. Walk you before you run son.
April 4, 2020 at 6:52 am #215334
Thanks a lot for such detailed explanation. Your explanation is brilliant.
April 4, 2020 at 2:10 pm #215376ParticipantTopics: 29Replies: 813Points: 2,450Rank: Community Hero
If you run it without the parentheses, you can see the definition:
$a = get-childitem file $a.create OverloadDefinitions ------------------- System.IO.FileStream Create()
Also, in emacs editmode, you can press tab to see all the properties and methods:
Set-PSReadLineOption -EditMode emacs $a. # tab LinkType FullName PSParentPath CopyTo InitializeLifetimeService Mode IsReadOnly PSPath Create MoveTo ModeWithoutHardLink LastAccessTime PSProvider CreateText Open Target LastAccessTimeUtc UnixStat Decrypt OpenRead Attributes LastWriteTime BaseName Delete OpenText CreationTime LastWriteTimeUtc Group Encrypt OpenWrite CreationTimeUtc Length Size Equals Refresh Directory Name UnixMode GetHashCode Replace DirectoryName PSChildName User GetLifetimeService ToString Exists PSDrive VersionInfo GetObjectData Extension PSIsContainer AppendText GetType
- This reply was modified 1 month, 4 weeks ago by js.
April 6, 2020 at 3:43 pm #215901ParticipantTopics: 6Replies: 93Points: 427Rank: Contributor
Emacs … Yee haw, have not heard of anyone using that in a while 🙂 I use VI myself.
April 7, 2020 at 5:13 am #216159ParticipantTopics: 3Replies: 340Points: 1,120Rank: Community Hero
In addition to the Out cmdlets, there is also New-Item and Set-Item. But to reiterate the advice here, learn how to explore and use powershell. Get-Member and Get-Help are absolutely crucial to answer the numerous questions such as the one you asked. There are also so many videos from Don’s public video’s on youtube, Microsoft Virtual Academy, and plenty of super stars out there. I believe I heard a certain training site has a free promotion this month that I think Don and many other Powershell godfathers have content on.
I hope this is helpful.
April 7, 2020 at 1:21 pm #216267ParticipantTopics: 29Replies: 813Points: 2,450Rank: Community Hero
set-psreadlineoption -editmode vi
- You must be logged in to reply to this topic.